Den virker bruger den selv :) ellers kik her ->
http://showsource.dk/php/?list=Logincreate.php
<html><head></head><body>
<form action="create-ok.php" method="post">
Username: <input type="text" name="username">
Password: <input type="password" name="password"><br>
<input type="submit" name="create" value="Create">
</form></body></html>
create-ok.php
<?php // PHP script start.
require("config.php"); // Variabler til mysql hentes
$create = &$HTTP_POST_VARS["create"];
if(empty($create)) {
print "You need to activate the script in the create.php."
?>
<br><a href="create.php">Click here to create user</a>
<?php
} else {
$username = &$HTTP_POST_VARS["username"]; // username from form i create.php.
$password = &$HTTP_POST_VARS["password"]; // Password from form i create.php.
if(empty($username) OR empty($password)) { //Here we check if one og the fields are empty.
echo 'One og the fields are empty';
}else {
// Here we open the mysql-server and we access it with the info from config.php.
mysql_connect($mysql_host, $mysql_user, $mysql_pw);
mysql_select_db($mysql_db); // Here we open the database defined in config.php.
$result = mysql_query("select username from users where username = '$username'") or die (mysql_error());
$number = mysql_num_rows($result);
if ($number > 0) {
print "Sorry the username are in use";
} else {
mysql_query("insert into users (username, password) values ('$username', '$password')") or die(mysql_error());
print "username $username are createt!";
}
?>
<a href="login.php">Click here to login</a>
<?php
}
}
?>
Config.php
<?php
// MySQL Variabler
$mysql_host = "localhost"; //Host to your mysql server.
$mysql_user = "username"; //Username to the mysql server.
$mysql_pw = "password"; //password to the mysql server.
$mysql_db = "database"; //datebase name to mysql server.
?>
login.php
<html><head></head><body>
<form action="login-ok.php" method="post">
Brugernavn: <input type="text" name="brugernavn"><br>
Password: <input type="password" name="password"><br>
<input type="submit" name="login" value="Login">
</form></body></html>
login-ok.php
<?php // PHP script start.
require("config.php"); // Variabler til mysql hentes
$login = &$HTTP_POST_VARS["login"];
if(empty($login)) {
echo 'This page require login'; }
else {
$username = &$HTTP_POST_VARS["username"]; // Brugernavn fra form i login.php.
$password = &$HTTP_POST_VARS["password"]; // Password fra form i login.php.
if(empty($username) OR empty($password)) { // Here we check if one og the fields are empty
echo 'One of the fields are empty'; }
else {
// Here we open the mysql-server and we access it with the info from config.php.
mysql_connect($mysql_host, $mysql_user, $mysql_pw);
mysql_select_db($mysql_db); // Here we open the database defined in config.php.
// If the username written in the field exist in the tabel, we poll this row.
$result_user = mysql_query("select username from users where username = '$username'") or die (mysql_error());
// Here stops the script if error from the mysql-server.
// If the password written in the field exist in the tabel, we poll this row..
$result_pw = mysql_query("select password from users where password = '$password'") or die (mysql_error());
// Here stops the script if error from the mysql-server.
// Here are the action, it checks igf the username or password are in the database.
// There is createt one array with the data we got from the mysql before.
$array_user = mysql_fetch_array($result_user); // First one with username.
$array_pw = mysql_fetch_array($result_pw); // and one with password.
// Here we checks if the array with username and password contain data
// PASSWORD CONTROL
if($array_user["username"] == $username AND $array_pw["password"] == $password) {
echo 'Correct password'; // Username and password are correct!!
} // Her lukker vi condition TRUE fra password kontrol
else { // Here starts condition FALSE on password control
echo 'wrong password';
} // Here we close condition FALSE on password control.
} // Here we close condition FALSE on empty check fields.
}
?>
login.sql
CREATE TABLE users(
id int(10) unsigned DEFAULT '0' NOT NULL auto_increment,
username varchar(16),
password varchar(16),
PRIMARY KEY (id)'