monkeynote Posted August 10, 2007 Share Posted August 10, 2007 hi guys! i am migrating my ASP code to PHP and I have a problem with redirection i have a code here that creates a cookie and redirects it automatically but the problem is that... on the first event that a user visits the page... the cookie is created but it does not redirect automatically. on the second event that a user visited that page, it redirects the user to another page. this code is just my example only <?php setcookie("name", "audemar", time() + 3600); // cookie creation setcookie("address", "myaddress", time() + 3600); setcookie("remarks", "cute", time() + 3600); if (isset($_COOKIE["name"])){ // checks if cookie does exist $name = $_COOKIE["name"]; $address = $_COOKIE["address"]; $remarks = $_COOKIE["remarks"]; header("Location: http://localhost:8080/testing.php?name=$name&address=$address&remarks=$remarks"); // redirects into another page } ?> thanks for reading my post and i hope that you can give me a solution with regards to my problem. thanks Quote Link to comment https://forums.phpfreaks.com/topic/64211-solved-redirection-problem/ Share on other sites More sharing options...
dbo Posted August 10, 2007 Share Posted August 10, 2007 Well it doesn't do it the first time b/c the test for isset($_COOKIE['name']) would return false... b/c it's not set. As for the second time your code looks ok at a glance. Try storing the header section in a variable and outputing it rather than redirecting. Make sure everything looks as expected. For example: header("Location: http://localhost:8080/testing.php?name=$name&address=$address&remarks=$remarks"); To $header = "Location: http://localhost:8080/testing.php?name=$name&address=$address&remarks=$remarks"; echo $header; Quote Link to comment https://forums.phpfreaks.com/topic/64211-solved-redirection-problem/#findComment-320102 Share on other sites More sharing options...
hvle Posted August 10, 2007 Share Posted August 10, 2007 Try to place the 3 setcookie lines after. There is no point setting the cookie then read it immediately after. Quote Link to comment https://forums.phpfreaks.com/topic/64211-solved-redirection-problem/#findComment-320116 Share on other sites More sharing options...
monkeynote Posted August 10, 2007 Author Share Posted August 10, 2007 what? php did not see my newly baked cookies during runtime? wow... its difficult for me to adjust myself to coding especially im used to create variables / cookies during runtime. in asp, when i create newly baked cookies... it automatically sees it when i search for it at once. <% response.cookies("name") = "monkeynote" response.cookies("name").Expires = date + 7 if request.cookies("name")<>"" then response.redirect ("http://url") end if %> Quote Link to comment https://forums.phpfreaks.com/topic/64211-solved-redirection-problem/#findComment-320120 Share on other sites More sharing options...
dbo Posted August 10, 2007 Share Posted August 10, 2007 I misread your code. If you've set them you should be able to read them. Quote Link to comment https://forums.phpfreaks.com/topic/64211-solved-redirection-problem/#findComment-320127 Share on other sites More sharing options...
monkeynote Posted August 10, 2007 Author Share Posted August 10, 2007 finally! the problem is solved! thanks for informing me guys that newly baked cookies is not set when i called it at once. what i did was 1.) created the cookie on first load. 2.) redirect the page to the new page that needs cookies 3.) check if the cookie is set 4.) if the cookie is not set... redirect it to redirect.php then create the cookie again considering that the browser cookies are enabled. thanks for the tips guys! Quote Link to comment https://forums.phpfreaks.com/topic/64211-solved-redirection-problem/#findComment-320131 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.