Avatar billede madsing Nybegynder
20. maj 2006 - 21:09 Der er 1 løsning

Mangler hjælp til login

Hej,

Jeg er igang med at lave et site, som skal indeholde et login system. Jeg har ledt efter et script på nettet, men kan ikke få dem til at fungerer, er der nogen som har eller kender et hvor der medfølger god vejledning.
Avatar billede fire-fox Nybegynder
21. maj 2006 - 05:28 #1
Den virker bruger den selv :) ellers kik her -> http://showsource.dk/php/?list=Login

create.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)'
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester