![]() |
![]() |
PHP Tutorial - Protecting a Page with SessionsDigg! This Page
index.phpFirst 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.phpThen, 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.
secret.phpOnce 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.logout.phpThen 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.CommentsComments:2nd Aug 2007 0:25HiIts 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 binish10th Aug 2007 13:09You 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 Anders14th Aug 2007 6:23Binish - 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 Hay5th Nov 2007 9:44Any Way to use an ID and Password for thisBy Robert26th Dec 2007 8:34Hey 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 HayAdd your comment... |
| 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) |
![]() ![]() |