Jump to content

Cookies


jarrett000

Recommended Posts

Hey, I have a Site where, at the beginning user accept a terms agreement and a box appears. I then use that box to say "Welcome $_GET['name']". I would like to set a cookie up where it remembers the name typed in. I've been checking out the cookies tutorial on w3schools but I tried it over 20 different ways probably now and I can't get it to work! Can someone help?

Link to comment
Share on other sites

The cookie will not be available on that page. You need to change pages or reload that page before the cookie information is ready to use.

Link to comment
Share on other sites

He means if you set the cookie in a script it won't be immediately available, you need to wait until another action (e.g. refresh) is performed to access the cookie.

set cookieyou won't see the cookie here immediately

Link to comment
Share on other sites

This may sound incredibly obvious, but if you are able to set the cookie in the first place, then you still have the data. Put it in a variable and use it. If it's one of those pages where you can either set the cookie or retrieve the cookie, then some sort of if/else or the ternary ?: would work. EG:$name = isset($_COOKIE['name]) ? $_COOKIE['name'] : $_POST['name'];echo "Hello, $name";Your code will be different, of course, but that's the idea.

Link to comment
Share on other sites

This may sound incredibly obvious, but if you are able to set the cookie in the first place, then you still have the data. Put it in a variable and use it. If it's one of those pages where you can either set the cookie or retrieve the cookie, then some sort of if/else or the ternary ?: would work. EG:$name = isset($_COOKIE['name]) ? $_COOKIE['name'] : $_POST['name'];echo "Hello, $name";Your code will be different, of course, but that's the idea.
Thank you! that worked, now theres another part to this script I cant figure out (completely) heres the proble: I have:
if (isset($_REQUEST['name']))header ('location: Home.php')

saying "if they've entered a name before, take them to the home (where it says welcome$_GET['name']" The problem is I have a failsafe on the page so If name isnt set it displays a blank page (return false;). The conflict is taking them to Home.php dosent set a ?name=name. So how do i make it say Home.php?name=(the name they entered here)? I've tried:

if (isset($_REQUEST['name']))header ('location: Home.php?name=$_REQUEST['name']')

But of course I get a T_STRING error. I've tired to work my way around the error but I just can't think of the right code.

Link to comment
Share on other sites

header ("location: Home.php?name={$_REQUEST['name']}")

BTW, you really shouldn't use the $_REQUEST array. It holds get, post, and cookie values. That sounds convenient, but it would be very easy to forget and have a post value and a cookie value with the same keyword. In that case, one would be overwritten, and you wouldn't know which. Stick with $_POST, $_GET, and $_COOKIE .

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...