By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,514 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,514 IT Pros & Developers. It's quick & easy.

how to direct to my own login page when user enter some other URL

P: 3
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.
Dec 10 '07 #1
Share this Question
Share on Google+
3 Replies


P: 41
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
Dec 10 '07 #2

P: 3
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

Hi,
I have still doubt could u please clarify. Where to start session in login page or in details page.
I followed apprach a using header i am still geting details page not redirecting to login page.
In Error log it shows the following message..
Warning: Cannot modify header information - headers already sent
Dec 11 '07 #3

P: 3
Hi,
I have still doubt could u please clarify. Where to start session in login page or in details page.
I followed apprach a using header i am still geting details page not redirecting to login page.
In Error log it shows the following message..
Warning: Cannot modify header information - headers already sent
I am using cookies is it right way or not please let me know.
it is working now.
I am calling set cookie method with some variable in login.php if the login is success , in details page i am checking whether this cookie is set or not.if it is not set then calling login page.
it is working i want to know whether it is right way or not.

Please let me know..
Dec 11 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.