0

I have a page that only allow the user accessing the page after login. Say that this page is

http://www.example.com/secret/data/important.php?id=23&pin=2if24scrtw2323.

This is the scenario: When the user access the URL without login, the user will be redirected to login page. My login page URL:

http://www.example.com/login.php

After successfully login, the user will be redirected again automatically to the page that he/she try access in advanced (in this case: > http://www.example.com/secret/data/important.php?id=23&pin=2if24scrtw2323) Just like facebook.

What's idea behind it? Thx

2
  • use cookies, or use the session (you would have to start it before the login) Commented Apr 11, 2013 at 9:41
  • Is there another way without using cookies or session? Commented Apr 11, 2013 at 9:47

6 Answers 6

2

You read the url you came to login page. It is stored in $_SERVER unless I am mistaken. (I would advise doing print_r($_SERVER) to see what's available) Alternatively, you bring $_SERVER['REQUEST_URI'] to login page as a parameter like so header('login.php?came_from=' + $_SERVER['REQUEST_URI']) when you redirect the user to login. Put it in the form as a hidden input. Then after login, you still have that url as a parameter in the script so you can now easily use header('yourpage.php') to redirect again :)

Sign up to request clarification or add additional context in comments.

Comments

1

One option is to store the redirect location somewhere. This can be done in the url or my preferred option: in a hidden field.

Comments

1

store the value of page in cookie

$_COOKIE['visit'] = "http://www.example.com/secret/data/important.php?id=23&pin=2if24scrtw2323"; and then from login header('location: '.$_COOKIE['visit']); 

Comments

1

first you need to check the id and pin value using php GET and POST or REQUEST like $_GET['id']

after successful login you can set values in session like $_SESSION['id'] and redirect thr user on agin or reload the page and check session value is exist or equel to get value

Comments

0

Please check the php manual for the header method, there is an example for redirection.

http://php.net/header

Copy&Paste from there:

<?php header("Location: http://www.example.com/"); /* Redirect browser */ /* Make sure that code below does not get executed when we redirect. */ exit; ?> 

Comments

0

Something like this:

<?php session_start(); if (!$_SESSION['logged_in']) { header('location: login.php'); die; } //other code ?> 

1 Comment

About header, i'm clear here. What I need is how to remember the url that the user visited before without using session & cookie so I can redirect him/her to that page

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.