Code:
<?
session_start(); //initialize session mechanism
if(!isset($_POST['ok'])) {
// if the form is not completed, display it
echo"
<html>
<head>
<title>Authorization page</title>
</head>
<body>
<table width='100%' height='100%'>
<form method='POST' action='login.php'>
<tr><td align=center>
<table>
<tr><td>
<table>
<tr><td>Login:</td><td><input type='text'
name='login' size='15'></td></tr>
<tr><td>Password:</td><td><input
type='password' name='pass' size='15'></td></tr>
</table>
</td></tr>
<tr><td align=center><input type='submit' name='ok'
value='Enter'></td></tr>
</table>
</td></tr>
</form>
</table>
</body>
</html>
";
}
else{
//supposed that user data
//is saved in database, in users table, that includes id, login, pass fields
$db=mysql_connect('host', 'login', 'password');
mysql_select_db('db_name', $db);
//check if there is a user with such login and password
$res=mysql_query("SELECT * FROM users WHERE login='".$_POST['login']."'
AND pass='".$_POST['pass']."'", $db);
if(mysql_num_rows($res)!=1){ //such user doesn’t exist
echo "Incorrect login and password";
}
else{ //user is found
$_SESSION['login']=$_POST['login']; //set login & pass
$_SESSION['pass']=$_POST['pass'];
Header("Location: protected.php"); // redirect him to protected.php
}
mysql_close();
}
?> This sample of php login script describes easy way of access control with convenient method of saving user data in database. Here is another simple php login script that saves all inputted login and password in arrays $_POST and $_GET Code:
<html>
<head><title>Welcome to PHP</title></head>
<body>
<?php
if ($login = $_POST["login"]) {
echo "<b>Привет, login!</b>";
}
?>
<form method="post">
your login: <input type="text" name="login"/><br/>
<input type="submit" value="Enter"/>
</form>
</body>
</html> Good luck.