spacepoet Posted November 27, 2012 Share Posted November 27, 2012 Hello: I am trying to redirect a user from one page when they click a link, and carry them to a new page with the session (or variable?) from the first page. I cannot seem to get this to work. What am I doing wrong. The idea is a user clicks a link like this: <a href="philadelphia-pa-19128.php">Philadelphia, PA 19128</a> Which goes to philadelphia-pa-19128.php: <?php $myLocation = "Philadelphia, PA 19128"; header('Location: locations-page.php'); ?> <html> ... </html> Then the header would - I thought, incorrectly - take them to a new page and print the location: <?php ?> <? $myLocation = $_REQUEST['myLocation']; ?> <html> <? echo "myLocation" ?> </html> This is not working ... How can I accomplish this? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/271225-redirect-page-and-carry-session-to-next-page/ Share on other sites More sharing options...
garbagedigger Posted November 27, 2012 Share Posted November 27, 2012 (edited) you are using $_REQUEST with a key (myLocation) that is not set. $_REQUEST looks for a post, get or cookie and you have not set this location key and it's value as a cookie or passed it in a post or get request. That is why you don't get anything back. So there are a few ways to address this. I will show you the GET way. simply add this line of code to whatever php page is serving up the link: echo '<a href="locations-page.php?myLocation='.urlencode('philadelphia-pa-19128').'">Philadelphia, PA 19128</a>'; Inside your locations-page.php add the following line: echo urldecode($_REQUEST['myLocation']); That should take care of it. You are sending the myLocation as a url param (?myLocation=) and getting the data based on the key 'myLocation'. You can also use $_GET to do the same thing. Edited November 27, 2012 by garbagedigger Quote Link to comment https://forums.phpfreaks.com/topic/271225-redirect-page-and-carry-session-to-next-page/#findComment-1395407 Share on other sites More sharing options...
Jessica Posted November 27, 2012 Share Posted November 27, 2012 There are so many issues here. Do you really have a page for each location? Please for the love of god learn how databases work, and mod rewrite. Quote Link to comment https://forums.phpfreaks.com/topic/271225-redirect-page-and-carry-session-to-next-page/#findComment-1395408 Share on other sites More sharing options...
requinix Posted November 27, 2012 Share Posted November 27, 2012 Two new posts? Meh. Not bothered enough to rewrite mine. You're sending them to "locations-page.php". There's nothing in there about "myLocation". Yes you set a variable, but that was on the previous page. The new page doesn't know about it unless you tell it. But if you're asking about stuff that's wrong, we have to step back a little further. It sounds like you're creating a bunch of PHP files for all the different sities and zip codes? That's a lot of files. Doesn't it sound a bit unreasonable to you? It is. The way it's actually done is with URL rewriting. The web server (like Apache) looks at the URL, sees that it fits a pattern, and rewrites it to a different URL. For example, it sees "/location/philadelphia-pa-19128" and silently turns it into "/locations-page.php?city=philadelphia&state=pa&zip=19128". Blah blah blah search for PHP and URL rewriting for plenty of information how to do it. Quote Link to comment https://forums.phpfreaks.com/topic/271225-redirect-page-and-carry-session-to-next-page/#findComment-1395411 Share on other sites More sharing options...
spacepoet Posted November 27, 2012 Author Share Posted November 27, 2012 you are using $_REQUEST with a key (myLocation) that is not set. $_REQUEST looks for a post, get or cookie and you have not set this location key and it's value as a cookie or passed it in a post or get request. That is why you don't get anything back. So there are a few ways to address this. I will show you the GET way. simply add this line of code to whatever php page is serving up the link: echo '<a href="locations-page.php?myLocation='.urlencode('philadelphia-pa-19128').'">Philadelphia, PA 19128</a>'; Inside your locations-page.php add the following line: echo urldecode($_REQUEST['myLocation']); That should take care of it. You are sending the myLocation as a url param (?myLocation=) and getting the data based on the key 'myLocation'. You can also use $_GET to do the same thing. Thanks for the idea! I will give this a shot. No, I do not have a page for every location - I am testing different ideas. The idea is to use my ZipCode database and use mod re-write; like I do in other projects. Quote Link to comment https://forums.phpfreaks.com/topic/271225-redirect-page-and-carry-session-to-next-page/#findComment-1395756 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.