tags:

views:

191

answers:

3

Lets say, user go to "profile.php" page, but it requires login. Then it redirects to login page. But after login, it should redirect back to profile.php page. How do i do that?

How to get current URL in php?

$_SERVER['URI']????

+1  A: 

To get current page url concat $_SERVER["SERVER_NAME"] and $_SERVER["REQUEST_URI"].

What you could do is create a session variable when you detect that user needs to sign in. Then redirect it to login page, handle signing and after you verify that his credentials were correct you can retrieve the value from the session and redirect him to the page he was requesting.

RaYell
+2  A: 

Most sites simply pass a variable to the login form like this:


redirect(login.php?returnUrl=original_page.php);

Then after the login is processed, it could redirect back to the page which returnUrl is pointing to. If there is no returnURl variable, then call the default one (index.php or default.php).

Hope this help.

Freddy
Unfortunately this makes your URLs ugly so I would rather use sessions for that purpose.
RaYell
session? Means, i need to set session before redirect to login page, unset session after redirect page??
bbtang
@bbtang Exactly that
RaYell
A: 

I don't know if this is the best method, but I used to solve this like this. In pages which requires login, say in your case "profile.php", i 'll issue a new session, just to remind the browser you cam from this page. And then in login page after validating the credential if the user is legitimate, then Ill check if there is any page specific session. If so I'll redirect to that page.

in pseudocode

First in profile.php

if(!isset($_SESSION['login'])
header('Location: login.php');

in login.php, after checking

if(isset($_SESSION['profile'])
header('Location: login.php');
else
header('Location: index.php');

It sometime since I used PHP, so the code may be rusty but you can follow a logic like this. Once again, there may be better options. Do research.

Christy John