Jump to content

Login Page Codes


Garnet

Recommended Posts

a very simple example, and not the most secure, but here's a start:page1.php will contain a user name and password form which submits to page2, if you don't know how to do that then you need to go back and read the tutorials. page2.php can look something like this:

<?php//start sessionsession_start();//create login variable$_SESSION['login']='no';//get inputted username and password$username = $_POST['username'];$password = $_POST['password'];//check username and password and set login variable if correctif $username == 'DarkElf'    {if $password == 'Spoon'        $_SESSION['login'] = 'yes';    };?>DOCTYPE....HTML headers.....<body><?php//check login variable and display content accordinglyif $_SESSION['login'] == 'no'    echo '<p>Sorry, you are not a recognised user, please go back and try again.</p>';else if $_SESSION['login'] == 'yes'    echo '<p>Welcome user..... more page content etc.</p>';?></body></html>

The second chunk of php needs to be included on every page of your site which you would like to be password protected. After succesfully signing in the user will be 'logged in' for the duration of the session (i.e. until they close that browser window).Please note that this is an extremely simple example, it only allows for one user and the username and password are hard coded into the php. You'll need to learn some considerably more advanced PHP and preferably a web database such as MySQL before you can start making genuinely useful login scripts.

Link to comment
Share on other sites

Darkelf you did this part wrong :) :

//check username and password and set login variable if correctif $username == 'DarkElf'   {if $password == 'Spoon'       $_SESSION['login'] = 'yes';   };?>

Its supposed to be:

//check username and password and set login variable if correctif $username == 'DarkElf'{   if ($password == 'Spoon'){       $_SESSION['login'] = 'yes';   };};?>

Just so he doesnt do that part wrong..

Link to comment
Share on other sites

  • 6 months later...

I don't think this works. I tried it out to test it and it doesn't.Maybe I messed up?

Link to comment
Share on other sites

Wow.. I looked at that and was like, is that really me? I guess I didnt know PHP as well :). Here is the corrected full script:

?php//start sessionsession_start();//create login variable$_SESSION['login']='no';//get inputted username and password$username = $_POST['username'];$password = $_POST['password'];//check username and password and set login variable if correctif($username == 'DarkElf'){   if($password == 'Spoon'){	   $_SESSION['login'] = 'yes';   }}?>DOCTYPE....HTML headers.....<body><?php//check login variable and display content accordinglyif($_SESSION['login'] == 'no'){   echo '<p>Sorry, you are not a recognised user, please go back and try again.</p>';}elseif($_SESSION['login'] == 'yes'){   echo '<p>Welcome user..... more page content etc.</p>';}?></body></html>

enjoi :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...