Last page visited ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
philippe70
Forum Newbie
Posts: 4
Joined: Sat Jan 15, 2005 1:41 pm

Last page visited ...

Post by philippe70 »

Code: Select all

if (isset($_SESSIONї'last_page'])) {
$last_page = $_SESSIONї'last_page'];
}

$current_page = $_SERVERї'PHP_SELF'];
if (isset($_SERVERї'QUERY_STRING'])) {
$current_page .= "?" . htmlentities($_SERVERї'QUERY_STRING']);
}
$_SESSIONї'last_page'] = $current_page;
When i print $last_page i get the Url of the current page!?!? You know what I've done wrong?

I want to get the url of the last page.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$last_page would equal $current_page after a page refresh..
philippe70
Forum Newbie
Posts: 4
Joined: Sat Jan 15, 2005 1:41 pm

Post by philippe70 »

tks for the quit reply.... but you have asuggestion... cause i'm really stock whit this problem... and i don't want to use Query String.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

avoid setting last_page on the page you don't have to overwrite the "history" information. Only overwrite it once you need to.

Maybe we aren't understanding fully. Could you explain what you are trying to do in detail, please?
philippe70
Forum Newbie
Posts: 4
Joined: Sat Jan 15, 2005 1:41 pm

Post by philippe70 »

1. I have a form

2. I want the action of the form sent the user to the page he was before the form.

3. So in one php page i include on every pages, I put this code, so i always have the last_page the visitor was on.

Code: Select all

if (isset($_SESSIONї'last_page'])) {
$last_page = $_SESSIONї'last_page'];
}

$current_page = $_SERVERї'PHP_SELF'];
if (isset($_SERVERї'QUERY_STRING'])) {
$current_page .= "?" . htmlentities($_SERVERї'QUERY_STRING']);
}
$_SESSIONї'last_page'] = $current_page;
4.

So my form look like this:

Code: Select all

<form name="festimail" method="post" action="<?= $last_page ?>">
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

why not store the url/file/whatever to redirect to stored in the form? It can be dynamic if the form is on multiple pages... potentially, you could store a list of all your pages in a database, or ghetto style it with just listing the specific url to redirect to like this:

Code: Select all

<form method="post" action="processing_page.php">
<input hidden="fromUrl" value="&#123;$_SERVER&#1111;'SCRIPT_NAME']&#125;">
...
you then just load the url in and redirect after processing..

Code: Select all

...
if(isset($_POST&#1111;'fromUrl']))
&#123;
  $urldata = parse_url($_POST&#1111;'fromUrl']);
  if(!is_array($urldata))
    $url = 'http://www.yoursite.com/your_default/page.php';
  elseif(preg_match('#.*\.yoursite\.com#i', $urldata&#1111;'host']))
    $url = 'http://' . $urldata&#1111;'host'] . $urldata&#1111;'path'] . '?' . $urldata&#1111;'query'];
  header('Location: ' . $url);
&#125;
philippe70
Forum Newbie
Posts: 4
Joined: Sat Jan 15, 2005 1:41 pm

Post by philippe70 »

Code: Select all

<?php 
session_start();

if (isset($_SESSION&#1111;'last_page'])) &#123;
$last_page = $_SESSION&#1111;'last_page'];
&#125;

$current_page = $_SERVER&#1111;'PHP_SELF'];
if (isset($_SERVER&#1111;'QUERY_STRING'])) &#123;
$current_page .= "?" . htmlentities($_SERVER&#1111;'QUERY_STRING']);
&#125;
if ($_SERVER&#1111;'PHP_SELF'] != '/festimail.php') &#123;
$_SESSION&#1111;'last_page'] = $current_page;
&#125;
'Voici' my solution! And i will use your tricks to redirect. tks!
Post Reply