PDA

View Full Version : php session, hold current url



gerbera
12-19-2010, 05:08 PM
Hey guys, I was wondering if there was an easy way, in a session variable, to store what url the user is currently viewing? Thanks for any help. :thumbsup:

gerbera
12-19-2010, 05:09 PM
or a way to remember last where the user was, so i can redirect them back there.

_Aerospace_Eng_
12-19-2010, 08:17 PM
You can use something simple which will just store the request like this

<?php
session_start();
$_SESSION['url'] = $_SERVER['REQUEST_URI'];
?>
or use a function for a location with a little more information.

<?php
session_start();
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
$_SESSION['url'] = curPageURL();
?>
The function about will give you http://, the host name, the port, and the current file requested. You could also use $_SERVER['HTTP-REFERRER'] however this may not always be provided because of the browser.

gerbera
12-19-2010, 11:43 PM
thanks so much, how come it's giving me this error...

Undefined index: HTTPS in /Applications/MAMP/htdocs/test.php on line 4

Inigoesdr
12-20-2010, 12:36 AM
The HTTPS index won't always be there. Try something like this:

session_start();

function currentPageURL()
{
$isHTTPS = (array_key_exists('HTTPS', $_SERVER) && !empty($_SERVER['HTTPS']));
$hostname = $_SERVER['SERVER_NAME'];
if (($isHTTPS && $_SERVER['SERVER_PORT'] != 443) || (!$isHTTPS && $_SERVER['SERVER_PORT'] != 80))
$hostname .= ':' . $_SERVER['SERVER_PORT'];

return ($isHTTPS ? 'https://' : 'http://') . $hostname . $_SERVER['REQUEST_URI'];
}
$_SESSION['url'] = currentPageURL();

gerbera
12-20-2010, 12:43 AM
thanks, another problem i'm having is that on another page, it's saying that session is not defined. even though the session is started. how do I make the function global so the session variable can be used on any page?

Inigoesdr
12-20-2010, 12:49 AM
You have to start the session (http://php.net/session_start) on each page you need to use session variables. If you have a common include on your pages, a config file for instance, you could start the session there. You can also set the PHP configuration variable session.auto_start (http://php.net/session.configuration#ini.session.auto-start) or use auto_prepend_file (http://php.net/ini.core#ini.auto-prepend-file) to specify a file to start it.

gerbera
12-20-2010, 12:54 AM
I am, does it matter if the files are in different directories?

Inigoesdr
12-20-2010, 12:56 AM
No, not by default.

gerbera
12-20-2010, 01:04 AM
the problem is, I use a file to verify the username and password (verify_user.php), and then if true redirect back to the previous page is what I want.... but the function just redirects to the verification file...

Inigoesdr
12-20-2010, 01:09 AM
the problem is, I use a file to verify the username and password (verify_user.php), and then if true redirect back to the previous page is what I want.... but the function just redirects to the verification file...
Explaining how it should work without posting the code that is not functioning correctly isn't going to get your problem solved.