Jump to content

[SOLVED] set a session when you use header redirect?


Recommended Posts

err... so I thought this was somehow affecting the header() instead of me having to do header_()... This defeats the whole purpose of doing this. If I was to do that, I might as well just set a session there too. It was to save me from going through and finding every spot that header was needing to be used.

err... so I thought this was somehow affecting the header() instead of me having to do header_()... This defeats the whole purpose of doing this. If I was to do that, I might as well just set a session there too. It was to save me from going through and finding every spot that header was needing to be used.

 

Not possible, as far as I know. Since PHP does not except overriding of functions. The other option, each page you set the "last_page" session before you call the header function. Either way you have to do some leg work.

You can check $_SERVER['HTTP_REFERER']. But that is dependent on a user. If they choose to shut that off, it will not send it etc. Very un-reliable.

 

If you really want to. At the top of each page, you can have 2 session variable settings.

 

'this_page'  and  'last_page'  if last_page is not set then set it to this_page. Then on page load, if last_page is set, you can store that somewhere for the page you are on in a variable then you set last_page to be this_page and this_page to be the page you are on.

 

Then in a regular variable you hold $lastpage which will contain the real last page for use on that page only.

 

A little crazy on the logic, but it would/should work.


Okay I'm basically trying what you have described premiso, which was the basic idea I had when I was trying to think of a new way to approach it. I have this for the top of my index page:

<?php
session_start();
header("Cache-control: private");

$lastpage = $_SESSION['last_page'];
$_SESSION['last_page'] = $_SESSION['this_page'];
if (isset($_GET['page'])){
$_SESSION['this_page'] = "$_SERVER[php_SELF]?page=$_GET

";
} else {
$_SESSION['this_page'] = $_SERVER['PHP_SELF'];
}

 

Trying to simply echo the lastpage variable when I'm on my login page. Here is what happens:

 

I'm on home page.

Go to contact page.

Go to login page.

 

---> displays: /performance/index.php?page=login

f5-> displays: /performance/index.php?page=contact

f5-> displays: /performance/index.php?page=login

 

How come I have to refresh to see "contact" as the previous page, when it was in fact the previous page. It has like a "lag" on it, do you see what is causing this?

<?php
session_start();
header("Cache-control: private");

$lastpage = isset($_SESSION['last_page'])?$_SESSION['last_page']:'';
$_SESSION['last_page'] = $_SESSION['this_page'];

if (isset($_GET['page'])){
$_SESSION['this_page'] = "{$_SERVER['PHP_SELF']}?page={$_GET['page']}";
} else {
$_SESSION['this_page'] = $_SERVER['PHP_SELF'];
}

echo "The last page you were on was: {$lastpage} <br /> The current page is: {$_SESSION['this_page']} <Br /> The new last page is {$_SESSION['last_page']}<br />";
echo "<a href=test.php?page=1>Page 1</a> | <a href=test.php?page=2>Page 2</a> | <a href=test.php?page=3>Page 3</a>";

die();
?>

 

I do not know what you are doing as you only show part of the code. Given the above code, everything works fine on my end, and as expected.

 

Output:

The last page you were on was: /test.php?page=3
The current page is: /test.php?page=3
The new last page is /test.php?page=2
Page 1 | Page 2 | Page 3

This was going from the "Page 3" link to the "Page 2"

It was a messup on my part.

 

The last page you were on was: /test.php?page=3
The current page is: /test.php?page=3
The new last page is /test.php?page=1
Page 1 | Page 2 | Page 3

 

That is going from Page 1 to Page 3.

 

It works as expected, I just mixed up the words. Give it a try and see, create a simple test.php and paste that code in it, and it should work like expected.

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.