Best method to return user after they login.

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
tuxiow
Forum Newbie
Posts: 8
Joined: Mon Nov 09, 2009 2:33 pm

Best method to return user after they login.

Post by tuxiow »

Hi guys,

What is the best method to return a user to the page they where on after they have logged in?

Thanks all.

Regards Paul
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best method to return user after they login.

Post by AbraCadaver »

Well, you can try redirecting back to $_SERVER['HTTP_REFERER'] but that isn't always set. So on each page in your site (except the login page) you can set a session var for the current page. Then on the login page you have that var that was set on the previous page.

-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Best method to return user after they login.

Post by pickle »

That only works if cookies & sessions are being used. I've seen some login pages that have "&return=/page/from/whence/the/user/came" in the URL. The login page can then read that from $_GET & redirect the user back.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Best method to return user after they login.

Post by AbraCadaver »

pickle wrote:That only works if cookies & sessions are being used. I've seen some login pages that have "&return=/page/from/whence/the/user/came" in the URL. The login page can then read that from $_GET & redirect the user back.
Good luck logging people in and maintaining some sort of state to tell if people are logged in or not without using sessions :-) You can use sessions without cookies, but you can't "log someone in" and keep their "logged in" state securely without using sessions.

Well, I guess you could append their username or id and a strong hash of their password (assuming the same hash is stored in the DB or whatever) to every link and form action, etc...on every page in your site/app after they log in, but that would be ridiculous!
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tuxiow
Forum Newbie
Posts: 8
Joined: Mon Nov 09, 2009 2:33 pm

Re: Best method to return user after they login.

Post by tuxiow »

Thanks for the advice guys. This is what I have come up with.

I have used this little function I found after some googling.

Code: Select all

<?php 
 
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; 
} 
 
?>
I saved it as getURL.php and included it in my page...

Code: Select all

include("getURL.php");
I then declared a variable from the function and then set it as a session so it can be picked back up as required.....

Code: Select all

session_start(); 
 
$returnURL                =    curPageURL(); 
$_SESSION['returnURL']    =    $returnURL;
Do you think this is okay or is there a better way.

Regards, Paul


Last bumped by tuxiow on Mon Dec 07, 2009 3:21 pm.
Post Reply