Jump to content

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/64211-solved-redirection-problem/
Share on other sites

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;

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
%>

finally! the problem is solved!

 

thanks for informing me guys that newly baked cookies is not set when i called it at once.  8)

 

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! :)

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.