ProDraz
ProDraz

Reputation: 1281

Redirecting to old link after logging in

I'm not sure which is the best way to do this, so I would love to hear some ideas.

Ok, let's say we have this link/url => www.myscript.com/clients.php?id=5

If I try to open it, I get redirected to www.myscript.com/login.php, because I'm not logged in, and then when I get logged in I don't get redirected to original link/url www.myscript.com/clients.php?id=5 as I would like to.

So what should I add to login.php to achieve such acting/working?

Upvotes: 0

Views: 114

Answers (3)

Grim...
Grim...

Reputation: 16953

I would change the bit of code that redirects you to the login page if you're not logged in.

Rather than redirecting to login.php, do this:

$myUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
header("location:login.php?from=".$myUrl);
exit;

On the login page, add $_GET['from'] to a hidden form element, or something.

Once the login is verified, redirect (remember to urldecode() first!).

Upvotes: 4

Brad
Brad

Reputation: 10690

You could use a cookie, or use GET.

Example using GET:

in clients.php:

if (!loggedIn()) {
header("Location: www.website/login.php?redirect=".$_SERVER['REQUEST_URI']);
}

then login.php could redirect back to the original page by redirecting to $_GET['redirect'].

You could also use cookies. In clients.php, store the url in a cookie, and then login.php can read the cookie and redirect back to the original URL.

Upvotes: 0

Shyju
Shyju

Reputation: 218852

in client.php, before redirecting to login.php, set the current page/url in a session variable. After successfully login, Check whether the session value exist for that purticular key (which we set from the client.php) and if yes , retrieve that value and redirect to that page.

Upvotes: 3

Related Questions