Home Page of Evan Islam
 
Bookmark and Share
Web Designer in Connecticut >> Web >> Create_a_Protected_Page_in_PHP
Last Updated: 2008/04/01 9:08:10pm

 

Easiest Way to Protect Page(s) using PHP and PHP SESSIONS

When I needed to create admin section for my first php driven website I was looking all over for a simple tutorial like this. There are tons of scripts out there for this function, but most of them use php and mySql database which was out of my league at the time. So I decided to simplify this so people in the beginning stage would benefit from it.

Create a new BLANK document in notepad called secured.php. Add the following code in the page >> Save >> Upload to your server.
Then access the file from your domainname.com/secured.php. Enter Username (admin) and Password (adminpass) and it should work :)

<?php
//always start session
session_start();

//lets look to see if form has been submitted with a field "username"
if(isset($_POST['username'])){
//you can change the username and password value here...

 if(($_POST['username'] == "admin") && ($_POST['password'] == "adminpass")){
  //if username and passwords match then create session called "secured"
  $_SESSION['secured'] = "Secured";
 }else{
// if the values don't match... show error message

  echo "Oop! Looks like the username and password is not what I'm looking for, sorry you can't continue :( <p>
  <a href='?'Lets try that again...</a>";
 }
}
//done creating session.
 
//if there IS NO sessions called "secured"
if(!isset($_SESSION['secured'])){
//then display a simple form
//feel free to customize your form nicely :)
echo "<form method='post'>
Username: <input type='text' name='username' maxlength='10' /><br>
Password: <input type='password' name='password' maxlength='10' /><br>
<input type='submit' value='login' />
</form>";
}else{
//else if the session is created... show me the HTML page :)
?>
 

 
<html>
<head>
<title>Session Login</title>
</head>
<body>
<p>You have been successfully logged in :)... now lets add some content in this secured page.
</p>
</body>
</html>
 
 
<?php
}
//this closes the else statement... DONE!!!
?>

If you have any questions regarding this tutorial, please send me an email at evan at evanislam.com. Goodluck :)

 


Comment by M
2009/02/02 07:09:01 AM
Works fine to me. Thanks.


Comment by harni
2009/02/10 08:10:56 AM
nice i want to logout function plz hel me


Comment by Evan Islam
2009/02/13 05:27:10 PM
Harni, Logout function is very easy...

Create a page called "logout.php" and add the following code in that page:
-----------------------
session_start();
if(isset($_SESSION['secured']))
unset($_SESSION['secured']);
?>
-----------------------
That's it... all you are doing here is unsetting the session 'secured'...
Hope this helps


Comment by Rashed
2009/08/05 01:23:52 AM
I am a bangladeshi student study in the php,javascript,mysql,html,css,ajax,xml,etc which is need to become a webdesigner or what i cannot understand? Are u suggest me a little?


Comment by William
2009/09/01 04:47:50 PM
please i created a blanc page in golive and name secured.php but when i go to source it wount work


Comment by William
2009/09/01 04:54:50 PM
thank its okay


Comment by aijaz khan
2011/07/03 12:38:14 PM

thanx tnxx......thanxxxx thank youuu very much..... love you...........bro

Previous Page

Got any Comments?

Your e-mail:

Your Name:

Website:

Type verification image:
verification image, type it in the box

Comment About This picture:

Evan Islam

My Portfolio
Facebook Graffiti Images
Drawings and Sketches
3D Modeling
Web Design
Connecticut Web Design
Scripts & Tutorials

About Evan
Connect with me on the web
Job History
My Blog
Reviews
My Friends
Calendar
Contact Me

Book Marking Sites
RSS FEEDS