jarrett000 Posted March 3, 2009 Share Posted March 3, 2009 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 More sharing options...
Synook Posted March 3, 2009 Share Posted March 3, 2009 Do you have an example of the code you tried? Link to comment Share on other sites More sharing options...
jarrett000 Posted March 3, 2009 Author Share Posted March 3, 2009 Yes, here is an example I got from w3schools: <?phpsetcookie("Name", "$_REQUEST['name']", time()+3600);?><html> etc. etc. Link to comment Share on other sites More sharing options...
jlhaslip Posted March 4, 2009 Share Posted March 4, 2009 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 More sharing options...
jarrett000 Posted March 4, 2009 Author Share Posted March 4, 2009 Well then, what page do you recommend putting it on? I've tried it on Index.php and Home.php Which are the only two pages associated with the cookie. Link to comment Share on other sites More sharing options...
Synook Posted March 4, 2009 Share Posted March 4, 2009 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 More sharing options...
jarrett000 Posted March 4, 2009 Author Share Posted March 4, 2009 Then is there a way to access the cookie immediately? Link to comment Share on other sites More sharing options...
jeffman Posted March 4, 2009 Share Posted March 4, 2009 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 More sharing options...
jarrett000 Posted March 4, 2009 Author Share Posted March 4, 2009 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 More sharing options...
jeffman Posted March 5, 2009 Share Posted March 5, 2009 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 More sharing options...
jarrett000 Posted March 5, 2009 Author Share Posted March 5, 2009 Thanks for the Info, I'll stick to $_POST, $_GET, and $_COOKIE. Thanks for the code that one {} did wonders. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.