Jump to content

Redirect after login


freeloader

Recommended Posts

Hi guys,

 

I ran into a little problem while coding a login script.

 

Current redirect after succesful login:

 

// Register $myusername, $mypassword and redirect to overview
session_register(myusername);
$_SESSION["myusername"] = $myusername;

header("location:overview.php");

 

Current schematics: users enter login data on index.php, form refers to checklogin.php, at checklogin we perform the login checks and then redirect to overview.php.

 

Now, what I want to accomplish is that when users get referred to the login by trying to access a page on the server (such as test.php), they get referred to that page again after login. I've seen it done on other sites, and I know a way to implement it here. I'm just trying to find out if there isn't a better/easier way to do this?

 

What I'd do is get the referer, and preg match it for my site's base url; if that's alright, it means the user came from my site, and we can safely redirect to the refererer. If not, he came from somewhere else and I redirect to overview.

 

Any other and better methods?

Link to comment
Share on other sites

Ok, if I'm understanding what you want. The user will attempt to access home.php, they will get a message saying you must be logged in to view this and get sent to the login page, you want it so that they get sent back to home.php (or whatever page redirected them to login.php). There's a couple of ways to do it, which you choose will depend greatly on your code. The most flexible options would be to set a $_SESSION variable before sending the user to the login page, then if that session variable isset you redirect to that page otherwise you redirect to a 'default' page.

 

 

Link to comment
Share on other sites

@ ym_chaitu

 

That's correct, I want to check if he's coming from the same site. Then redirect him back to the page he was trying to access.

 

@ gevensen

 

Thank you for the kind attempt to help, but I think you misunderstood my question. I know how to handle refererer :)

 

@  cags

 

Thank you for that input, that's indeed a good option. The only problem is that I had this script for a while and was dumb enough to not code it with headers and footers... meaning I'd have to change all pages for that. I thought about redirecting that way (or a comparable GET method) before as well.

 

If you think that's the best way to do this, I'll go ahead and do it, it's quite a lot of pages though... :)

Link to comment
Share on other sites

Solution so far, based on your input:

 

top of index.php:

<?php
/**** Set refererer ****/
session_start();
$_SESSION["refer"] = $_SERVER['HTTP_REFERER'];
?>

 

checklogin.php:

$domain = parse_url($_SESSION["refer"]);
$domain = str_replace("www.", "", $domain['host']);
$host = str_replace("www.", "", $_SERVER['HTTP_HOST']);

if($domain == $host) {
header("location:".$_SESSION["refer"]);
}
else{
header("location:overview.php");
}

 

Only thing I lack in this code is when I'm not logged in and I go to a specific page, I get redirected to login but it won't store referer in that case and thus will not bring me to my originally preferred page.

Link to comment
Share on other sites

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.