<?
include("conn.php"); // create a file with all the database connections
if($do_register){ // if the submit button were clicked
if((!$name) || (!$email) || (!$age) || (!$login) || (!$password) || (!$password2)){
print "You can't let any fields in blank.\n"; // if the user did not put some field
exit;
}
$name = stripslashes($name);
$email = stripslashes($email);
$age = stripslashes($age);
$login = stripslashes($login);
$password = stripslashes($password);
$password2 = stripslashes($password2);
// this is for security reasons
if($password != $password2){ // if passwords didn't match
print "The password and the confirmation are not the same!\n";
exit;
}
$password = md5($password);
mysql_query("INSERT INTO table (name,email,age,login,password) VALUES ('$name','$email',$age,'$login','$password')") or die (mysql_error());
print "Done!\n"; // if its okay, show this message
exit;
} // close the first "if"
?>
<form action="register.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
Age: <input type="text" name="age"><br>
Login: <input type="text" name="login"><br>
Password: <input type="password" name="password"><br>
Password Again: <input type="password" name="password2"><br>
<input type="submit" name="do_register" value="Sumbit">
</form>
And now, login.php:
<?
include("conn.php");
if($do_login){
$login = stripslashes($login); // VERY IMPORTANT FOR SECURITY OF YOUR DATABASE DON'T ERASE IT
$passwd = stripslashes($passwd); // VERY IMPORTANT FOR SECURITY OF YOUR DATABASE DON'T ERASE IT
$check = mysql_query("SELECT * FROM table WHERE login='$login' LIMIT 1;");
$user = mysql_fetch_array($check);
if($user[password] == md5($passwd)){ // if the writed password and the db password are the same...
setcookie("login","$login",time()+360000);
setcookie("pass","$passwd",time()+360000);
// ...set the cookies...
header("Location: userspage.php"); // ...and redirect to restrict page
}else{
print "Login or password incorrects!\n";
exit;
}
}
?>
<form action="login.php" method="post">
Login: <input type="text" name="login"><br>
Passwd: <input type="password" name="passwd">
<input type="submit" name="do_login" value="Log-in!">
</form>
And finally, userspage.php:
<?
if(isset($HTTP_COOKIE_VARS["login"])){
?>
Page contents here
<?
}else{
?>
This page is restrict for registered users only!
<?
}
?>
Here we are, its a very simple login sistem and you can put more things if u want. Maybe latelly I make an administration page and put here to. If u found any problems with this code, tell me and I'll fix! \_
-------------------- Edit -------------------------
Field password2 was in type="text". Changed by type="password".
Field "login" was name="name". This litle mistake caused very much problems!
Sumbit button was wrong. I write type="sumbit". Change by type="submit".


