dalehay.com
Search Engine Optimization

PHP Tutorial - Protecting a Page with Sessions

Digg! This Page

index.php

First of all, create a basic form that goes to login.php.
<form action="login.php" method="POST">
Pass Code: <input type="password" name="passcode"><br>
<input type="submit">
</form>

login.php

Then, on the login.php page we need to first open PHP tags, and start by adding ob_start(); (this allows a header location tag to be placed anywhere in a script without it showing errors). Then beneath it, you need to start the session ( session_start(); ). Now that is done, next we'll need to make an IF statement asking if the passcode given from the form on index.php matches the value we give it (In the example, 12345 is used). Then set the sessions name as 'canview' and the value of it to "yes", this'll set the session called canview with a value of 12345. Beneath that you will need to then add a header(); tag, redirecting you to the secret page.
If you enter the wrong information then you want the script to give an error, so add an ELSE statement underneath and within it add a die(); tag.
<?php
ob_start();
session_start();
if($_POST[passcode] == "12345") {
     $_SESSION['canview'] = "yes";
     header("Location: secret.php");
} else {
     die("Wrong Passcode");
}
?>

secret.php

Once you have entered the correct information you will be forwarded to secret.php and that is where your secret information will be viewable, if you have entered the correct passcode. Any information you wish to protect, then place it between the IF and } ELSE { lines.
Code:
<?php
session_start();
if($_SESSION['canview'] == "yes") {
     echo "This is a secret page!!";
} else {
     die("Please login");
}
?>

logout.php

Then to logout, that is very simple, all you do is make a link to a page called, lets say, logout.php and add a session_destroy() tag.
Code:
<?php
session_start();
session_destroy();
echo "You are now logged out.";
?>


Comments

Comments:

2nd Aug 2007 0:25

Hi
Its nice that know about the codes. But i can't run it by pasting the codes into the editor. There is 3 warning that is like

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at D:\projects\testing\bineesh\creation\login.php:9) in D:\projects\testing\bineesh\creation\login.php on line 11

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at D:\projects\testing\bineesh\creation\login.php:9) in D:\projects\testing\bineesh\creation\login.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at D:\projects\testing\bineesh\creation\login.php:9) in D:\projects\testing\bineesh\creation\login.php on line 14



Thanking You

Binish
By binish

10th Aug 2007 13:09

You must make sure there is no whitespace before or after the php tags, and you must put the session stuff in a php block at the top of the file. like so:

---------
<?php
/* do session stuff... */
?>
<html>
....
</html>
----------
By Anders

14th Aug 2007 6:23

Binish - If you have used ob_start() then you shouldn't get any session errors, however as Anders has mentioned, make sure it is at the top of the page.
By Dale Hay

5th Nov 2007 9:44

Any Way to use an ID and Password for this
By Robert

26th Dec 2007 8:34

Hey Robert,

Yes you can use this for a login system. You would have to connect to the database, then get it to read from the database and have the session value as the persons user id number. I will make a small tutorial on a login system soon. :)
By Dale Hay

Add your comment...

Your Name:

Your Email:

Your Comment:

Enter This Number:

(20551)


Note: Your IP address is saved into the database when you submit a comment. Any type of threatening behaviour will result in your ISP being contacted and legal action being taken place!




Latest News Stories
» Internet Explorer 8 Problems
» October
» Relaxtion by Dale Hay
» BBC Weather Revamp
» Dog Singing
» Windows 8
Top Viewed Stories
» PHP Mini Message Board Tutorial (3,978)
» PHP Search Engine Tutorial (2,140)
» Helen Willetts Pregnant? (1,216)
» Create your own Image Uploader (805)
» QI Series 6 on BBC One (747)
» Sudoku (668)
Most Commented Stories
» PHP Mini Message Board Tutorial (33)
» Create your own Image Uploader (18)
» I like my DuMP (15)
» I'm on Wikipedia! (7)
» Chocie Munchin Niece (6)
» Life on Mars... (6)