2

I want a script that firsts verifies that $_COOKIE['location'] is set. If $_COOKIE['location'] is not set, I want to save the entrance URL into $_COOKIE['EntryURL'] and set $_COOKIE['location'] to 'TRANS' while redirecting user to enter their zip or manually select their location.

So I have generated the following PHP:

if(!isset($_COOKIE['location'])) {

    $EntryURL = $_SERVER['REQUEST_URI'];
    if($EntryURL == '/') { 
        $EntryURL = '/index.php?pg=Home'; 
    }

    setcookie('EntryURL', $EntryURL);
    setcookie('location', 'TRANS');
    header("Location: MYSITE/index.php?pg=" . urlencode('Please Select Your Location')); 

}

The problem that I am encountering, however, is that all of this code is being executed with or without $_COOKIE['location'] being set. The page opens, sees $_COOKIE['location'] is not set, sets $_COOKIE['EntryURL'] and sets $_COOKIE['location'] and properly redirects.

However, on the redirect page (which is included from the index, meaning that the same code will be called again) it STILL executes the same script, generating a second cookie for each and prevents the redirect code on my location query page from working properly (as it is accessing the $_COOKIE['EntryURL'] which has been rewritten, rather generated again as it is saved twice, to reflect the $_SERVER['REQUEST_URI'] from the location page.

I CANNOT understand how it is possible to execute this code block with or without location cookie being set. Hopefully I am doing something stupid that someone can quickly identify.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
BJC
  • 23
  • 5
  • The workaround in the accepted answer here might help. http://stackoverflow.com/questions/10738593/check-if-a-php-cookie-exists-and-if-not-set-its-value – Michael Hommé Jul 12 '16 at 18:05
  • That in no way answers this problem. That person is trying to recall a cookie set on the same page, that is not what I am doing here. – BJC Jul 12 '16 at 18:06
  • What if you gave it an expire time? For example using `setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");` – nviens Jul 12 '16 at 18:11
  • It doesn't matter when it expires. In fact, I specifically want to go back and save these cookies for like six months so that the person does not have to reset their location each time they visit the site, provided they do not clear their history. My problem is that this code executes before the cookie exists and then again after it exists. – BJC Jul 12 '16 at 18:13
  • You didn't happen to disabled cookies while testing, did you? – Michael Hommé Jul 12 '16 at 18:16
  • The cookie is saving twice, cookies work. – BJC Jul 12 '16 at 18:17
  • On the next page, you are checking that the cookie does not exist, correct? – nviens Jul 12 '16 at 18:18
  • The code above is at the top of my index page, so it will be called on every page I make. It should only execute if `$_COOKIE['location']` is not set. When it executes, it should set the cookie (and it DOES) it is just that on the next page, it executes the same code and recreates cookies, now with a bad redirect url... – BJC Jul 12 '16 at 18:21
  • Have you tried echoing out the `$_COOKIE['location']` in your loop? Or in an `else` statement? – nviens Jul 12 '16 at 18:34
  • Post a screenshot of cookies in dev tools. – Michael Hommé Jul 12 '16 at 18:38
  • Not completely sure what you are asking me. But, I actually look up the cookies stored in the browser. I have deleted them and tried to echo `$_COOKIE['location']` before and there is nothing there. I have stripped everything out so that it would echo 'cookie is there' or 'cookie is not there'. It is able to identify if the cookie is there or not. Yet when the page redirects, it write the cookies again. So on my location page, I have two cookies each of location and entryurl. – BJC Jul 12 '16 at 18:39
  • pmahomme, you want me to show the cookies as they are actually stored on my computer from the browser? – BJC Jul 12 '16 at 18:41
  • Check out this solution given here http://stackoverflow.com/questions/15703487/failing-to-set-cookies-in-php?rq=1 – nviens Jul 12 '16 at 18:47
  • I have screen shots, but I'm new, I don't know how to upload. lol – BJC Jul 12 '16 at 18:48
  • You can add the images to your question – nviens Jul 12 '16 at 18:50
  • it won't upload the pictures, and really, I don't know why it matters. I delete cookies. there are no cookies. cookies work. I open the root of my site. I have two location cookies and two entryurl cookies. – BJC Jul 12 '16 at 18:59
  • Have you tried to use the 4th argument of `setcookie`? Pass "/" for that to make sure your cookie is available across your domain, and not only on the current location (which is the default): `setcookie('EntryURL', $EntryURL, 0, "/");` – trincot Jul 12 '16 at 19:01
  • that should not matter as I am not leaving this directory, ever. every page on my site is generated from /index.php – BJC Jul 12 '16 at 19:02
  • i am not sure (I am no expert), but i think adding the fourth argument would overwrite the cookies so that i would not get duplicates, but they would still be wrong. – BJC Jul 12 '16 at 19:03
  • But my question was: have you tried... – trincot Jul 12 '16 at 19:04
  • actually, to clarify, i have a process.php to execute scripts between pages if needed. but that doesn't really matter. – BJC Jul 12 '16 at 19:04
  • for the sake of the argument, i just set those parameters, exact same results, which is why i had not tried that previously, it has nothing to do with my problem. – BJC Jul 12 '16 at 19:06
  • i was wrong about overwriting the cookies, actually, there are still two of each. – BJC Jul 12 '16 at 19:07
  • Ryan, the cookies are retrievable on the page that it is redirected to. I can't test to see if they come back before the if because they do not exist before then. – BJC Jul 12 '16 at 19:14
  • @BJC I can't reproduce this by putting the code you've provided on two separate pages and running them. Only one version of each cookie. What browser are you using? – Michael Hommé Jul 12 '16 at 19:24
  • Post the screenshots here: http://imgur.com and post the links. – Michael Hommé Jul 12 '16 at 19:26
  • i am currently testing this in firefox. i want to make sure i am being clear that each page is /index.php?pg=1 and /index.php?pg=2 etc. but that should not matter. i have even tried setting the cookies and redirecting to a third page just to redirect back to the second in case i was trying to recall the cookie too fast. i have no idea what is happening. – BJC Jul 12 '16 at 19:27
  • i guess this is what you want to see: http://imgur.com/a/ktEMp – BJC Jul 12 '16 at 19:34
  • the first EntryURL cookie is the correct URL location. the second is wrong as it is grabbing the url of the redirect page, which should not happen, because that code should not execute when a location cookie set. – BJC Jul 12 '16 at 19:36
  • also odd to me, if the code is executing again, it should be getting stuck in an infinite loop, and that isn't happening... – BJC Jul 12 '16 at 19:45
  • if feel like everyone has given up on me. lol. but here is the even more effed up thing. i put a script to send me an email that says 'cookie not set' from within that loop and i loaded my webpage. i received 3 emails. – BJC Jul 12 '16 at 20:15
  • So it's going into a loop... have you tried using two separate files, going from the index to file2? And seeing what happens with that? – nviens Jul 12 '16 at 20:42
  • So, I have asked my index page to simply connect to database, insert a line of text into a sql database and die(); it does this three times. and that is before I even get to the loop. I just die() at the top of the page after the connection and it is being loaded 3 times. WTF? – BJC Jul 12 '16 at 21:46
  • Hmm... Try not redirecting from this page and just create the cookies in the if statement. Then try loading the page from your url by typing it back in on your browser. What happens if you do that? – nviens Jul 13 '16 at 13:59

0 Answers0