473,394 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
3 2937
clai83
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
pveena
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
pveena
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

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

Similar topics

4
by: XP | last post by:
I am having problems with the blow page. I have a login page where I enter the userid and password. This then connects to this page. If I enter the userid and password correctly, it prints...
1
by: Wayne Smith | last post by:
Applies to: Microsoft FrontPage 2000, Microsoft Access 2000, IIS 5.0 Operating System: Microsoft Windows 2000 Professional I am trying to protect a portion of a web site by allowing users to...
2
by: Gill Bates | last post by:
I'm trying to login to a banking site (https://www.providentconnection.com) using vb.net. I've tried many variations of WebClient and HttpWebRequest; none of which I've got to work. My latest...
7
by: Grant Merwitz | last post by:
Hi I am trying to get the enter key to submit my login form The login form is currently in a control on the page and uses an asp:imagebutton as it's login button. If a user presses enter...
10
by: et | last post by:
I have an asp.net program that uses a connection string, using integrated security to connect to a sql database. It runs fine on one server, but the other server gives me the error that "Login...
6
by: =?Utf-8?B?UGFyYWcgR2Fpa3dhZA==?= | last post by:
Hi All, We have a requirement where we have to develop a custom Login Page which will accept user's NT credentials ( Username , password, domain name). This then needs to be passed to a website...
4
by: jobs | last post by:
Hello. If my users are logged in, and try to access restricted pages I want to direct them to a custom 403 page. If they are not logged in, I would like to continue to direct them to the login...
2
by: upendrajpr | last post by:
Dear frindes, I am new to php. in my 'php' code when someone login into my page he/she awail the necessary page rights, it's quite ok. but when it click on the page and someone direct copy & paste...
2
oranoos3000
by: oranoos3000 | last post by:
hi I have a login page in my site for login member I want to any user that arrival correctly directly go to page1.php I use header statement(header("Location: page1.php")) in middle page that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.