Jump to content

Redirect Page And Carry Session To Next Page .. ?


spacepoet

Recommended Posts

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!

Link to comment
Share on other sites

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 by garbagedigger
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.