0

I have a PHP script that stores a url from which a form was submitted in a session variable and then compares it to the current page url. If it's not the same as the one stored in the session it gets unset, at least that's how it should work. The problem is that the if statement used for checking if the two urls match seeems to be ignored and the session vars get unset anyway.

$compare_url_old = array_shift(explode(',', $_SESSION['search_page']));
$compare_url_new = array_shift(explode(',', $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));

if ($compare_url_new != $compare_url_old)
{
    unset($_SESSION['search_page']);
    unset($_SESSION['search_price_min']);
    unset($_SESSION['search_price_max']);
    unset($_SESSION['search_name']);
}

The strange part is that if I try to echo something within the if statement it works properly but for some reason the unset functions get called every time despite the result.

  • 1
    What's the outcome of `var_dump($compare_url_old, $compare_url_new);`? – Quasdunk Feb 02 '13 at 13:23
  • string(27) "take.bg/gps_navigatsia__135" string(27) "take.bg/gps_navigatsia__135" Both urls are the same. – Nikolay Ivanov Feb 02 '13 at 13:31
  • You do have a `session_start()` in there right? – leftclickben Feb 02 '13 at 13:33
  • Yes I do, session vars transfer over to other pages properly if I remove the unset functions. – Nikolay Ivanov Feb 02 '13 at 13:36
  • If both values are equal, there is no way the if-condition is entered... The problem must be somewhere else. What about `var_dump($compare_url_new != $compare_url_old);`? – Quasdunk Feb 02 '13 at 13:41
  • It returns bool(false), if I comment out the unset function the vars remain. – Nikolay Ivanov Feb 02 '13 at 13:45
  • That's really strange... Is this code above really exactly as it is in your project? What if you just try `if (false) { ... }` - are the variables still being unset? – Quasdunk Feb 02 '13 at 13:57
  • Nope, there must be something wrong with the way I get the new and old urls. – Nikolay Ivanov Feb 02 '13 at 14:07
  • I once had a same problem, but not exactly like this. Try to `urldecode()` the `$compare_url_new`. I'm not sure if this is going to help, but it worth a try. – MahanGM Feb 02 '13 at 14:12
  • So far no luck, I've spent the whole day looking into this issue. I'll just find an alternative way to accomplish this functionality. – Nikolay Ivanov Feb 02 '13 at 15:58
  • Try to set $compare_url_new='1'; $compare_url_old='1'; before if. And check, whether if statement works correctly. I am sure, that php is ok, something is wrong with the values. – user4035 Feb 02 '13 at 19:23

1 Answers1

0

Problem solved, turns out it was something I overlooked above the code in question, the script works fine. Sorry for that.