Technology
Would you like to react to this message? Create an account in a few clicks or log in to continue.


Host By : Shwe Yaung Myanmar
 
HomeAdvertisingSearchLatest imagesRegisterLog inAdvertising Space

 

 PHP Single Login Script

Go down 
AuthorMessage
Admin
Admin



Posts : 49
Join date : 2008-01-09

PHP Single Login Script Empty
PostSubject: PHP Single Login Script   PHP Single Login Script EmptySun Apr 06, 2008 7:12 am

<?php
session_start();
$name[] = "Chris"; $pass[] = "123456";
$name[] = "Test"; $pass[] = "weewee";
$name[] = "blah"; $pass[] = "moo";
#create an array of usernames and passwords
#add as many as you want

function loginform(){ #this is the form the user sees if they are not logged in
echo '<fieldset style="padding: 2">';
echo '<legend>Login</legend>';
echo '<form method=post action="?">';
echo 'Name: <input type=text name=username value="' . $_POST[username] . '">';
echo '<br>';
echo 'Pass: <input type=password name=pass>';
echo '<br>';
echo '<input type=submit name="s1" value="Login">';
echo '</form>';
echo '</fieldset>';
}

#used for the logout link, changes the session 'loggedin' = false if ?action=logout
if($_GET['action'] == "logout"){
$_SESSION['loggedin'] = false;
$_SESSION['username'] = "";
}

if($_SESSION['loggedin'] == false){ #checks if the user isnt logged in
if(!$_POST['s1']){ #checks if the form has been submitted, if not show it
loginform();
}else{
#username check
$tmpname = $_POST['username']; #the username the user has submitted
$tmppass = $_POST['pass']; #the password the user has submitted

$t = count($name); #count the total users
$i = 0;
#create a loop to go through each username/password and compare it with the name pass which was submitted
while($i <= $t){
if($tmpname == $name[$i] && $tmppass == $pass[$i]){
#if the name/pass submitted matches any of the username and passwords set the session 'loggedin' = true
$_SESSION['loggedin'] = true;
$_SESSION['username'] = $name[$i];
header('Location: ?'); #reload the page
}
$i++;
}
echo "invalid name and password Surprised"; #show an error message if the user enters an incorrect name/pass
loginform(); #show the form
}
die;
}
?>

Welcome <?php echo $_SESSION['username']; ?>. anything put here is only visible to users who are logged in
<br>
<a href="?action=logout">Logout</a>
Back to top Go down
https://shweyaungmyanmar.board-directory.net
 
PHP Single Login Script
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Technology :: Web Technology Zone :: PHP Script-
Jump to: