Hi,
I am new to php. I have two pages.one is login page other is details page.
When user enter detaisl page directly in URL i want to redirect to login page then login success it should go to detaisl page.
Could any one help me out.
I tried using a variable declaring , assigning in login page.
checking the value in details page.
but i am getting error as variable not defined if open directly details page.
Hi,
I think what you are trying to do is, you have a members section that can only be accessed after you login. Now if someone were to try to view a page in the members area and did not login then it would redirect the user to the login page. Now once the user is authorized then the user is redirected back to page he just came from.
Correct so far?
A. To redirect in PHP use the header() function.
[PHP]
<?php
header("Location: login.php");
?>
[/PHP]
Make sure that there is no white space, or anything sent to the browser before the header function like include or require statements, otherwise it will not work. If you can't change your code and you have to have a include or require statement before the header function the use the output buffering functions ob_start() and ob_flush(). Check out www.php.net for a description of that.
B. Telling the login page where your user came from
There are 2 ways you can do this.
1. Use hidden forms on all your pages.
2. Use PHP SESSIONS.
For number 1, you can set up a hidden form on each of your pages that identifies it. The if someone goes to that page without logging in, you can redirect him to the login page with the hidden form value and process it there
I prefer number 2, but if you want to use SESSIONS you gotta read up the topic yourself.
In everypage of your members area you need to do the following
[PHP]
<?php
//start your session
session_start();
//There are 2 cases here.
// 1. the user has visited this page for the first time.
// 2. loggin in at some point, but logged out or his session expired
//Check if there is a session variable set or not
//and if not then set a sesesion variable and the redirect
if ( (isset($_SESSION['logged_in'])) {
//if user is not logged in then set a session variable
//and redirect
if (!$_SESSION['logged_in']) {
//store the page that the user was in
$_SESSION['previous_page'] = $_SERVER['PHP_SELF'];
header("Location: login.php");
}
else {
//do some user authenication
}
}
else {
//store the page that the user was in
$_SESSION['previous_page'] = $_SERVER['PHP_SELF'];
//redirect
header("Location: login.php");
}
?>
[/PHP]
This is by no means secure or complete. Just the basics.
hope this helps