Jump to content


Photo

Redirect on login (general idea).


  • Please log in to reply
4 replies to this topic

#1 Chesso

Chesso
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 31 July 2006 - 02:30 AM

Just wondering what people do about this? Like if a user tryed to access an area that required sign in and were redirected to the login page, how would you redirect them back to the page they wanted to access afterwards?

Iv'e tryed a few things but never got it to work properly.

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 July 2006 - 09:20 AM

Either set a cookie or a session which will hold the location they was before being redirected to the login page. Then when they successfully login you check whether the redirect cookie/session is set. If it is you redirect them, if its not you redirect them to your homepage.

#3 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 31 July 2006 - 10:08 AM

The way I do it is to have a call to an authorize function near the start of the script for each page that is restricted to members only. The function returns true or false and does three things.  First it checks to see if the user is logged in. If so it returns true. If not, it checks to see if a valid username and password was supplied, and if so it logs the user in and returns true. Otherwise it renders a login form and returns false - the "action" parameter of the form is set to the same page as the calling script using the $SERVER["PHP_SELF"] superglobal.  On return from the function, if the return was false, I just end the script there instead of rendering the rest of the page. So, as soon as the user successfully logs in they will be returned to the same page which is then rendered fully.

It works for me, it may not be the way everyone does it.
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.

#4 Chesso

Chesso
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 10 August 2006 - 02:08 AM

Well I was using __FILE__ in combination with $_SERVER['QUERY_STRING'] but I can't test locally (which I need to) because instead of using http://localhost it puts the literal location (I can't use PHP_SELF because it doesn't include the site url like www.chessoscorner.com for example).

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 10 August 2006 - 11:09 AM

$_SERVER['PHP_SELF] retrieves the relative path to the servers document root (the main directory your files get uploaded to for your website). If you want to have your sites url prepended before $_SERVER['PHP_SELF'] use:
$url = $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];

// now $url holds the url and the location of the script
echo $url;





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users