Jump to content

Archived

This topic is now archived and is closed to further replies.

Chesso

Redirect on login (general idea).

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
$_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:
[code=php:0]$url = $_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];

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

Share this post


Link to post
Share on other sites

×

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.