PHP tutorial - Come creare un semplice sistema di login
Questo script è composto da 4 file:
1. main_login.php
2. checklogin.php
3. login_success.php
4. logout.php
Procedimento.
1- Noi abbiamo utilizzato un database chiamato test, in cui abbiamo creato un tabella chiamata members.
Questo è il codice sql per creare la tabella:
-
CREATE TABLE `members` (
-
`id` INT(4) NOT NULL AUTO_INCREMENT,
-
`username` VARCHAR(65) NOT NULL DEFAULT '',
-
`password` VARCHAR(65) NOT NULL DEFAULT '',
-
PRIMARY KEY (`id`)
-
) TYPE=MyISAM AUTO_INCREMENT=2 ;
-
-
--
-
-- Dumping data for table `members`
-
--
-
-
INSERT INTO `members` VALUES (1, 'admin', 'admin');
Quindi per effettuare il login dovremo utilizzare come nome utente: admin e come password: admin
2 - Creiamo il file main_login.php
e copiamo al suo interno il seguente codice:
-
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
-
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
-
</tr>
-
<td width="78">Username</td>
-
<td width="6">:</td>
-
</tr>
-
<td>Password</td>
-
<td>:</td>
-
</tr>
-
</tr>
-
</table>
-
</td>
-
</form>
-
</tr>
-
</table>
3 - Creiamo il file checklogin.php e inseriamo al suo interno il seguente codice:
-
<?php
-
$host="localhost"; // Hostname
-
$username=""; // Mysql username
-
$password=""; // Mysql password
-
$db_name="test"; //Nome del Database
-
$tbl_name="members"; // Nome della Tabella
-
-
// Procedimento per connettersi al Database
-
-
// Nome utente e password inviate attraverso il form
-
$myusername=$_POST['myusername'];
-
$mypassword=$_POST['mypassword'];
-
-
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
-
-
// Mysql_num_row is counting table row
-
// If result matched $myusername and $mypassword, table row must be 1 row
-
-
if($count==1){
-
// Register $myusername, $mypassword and redirect to file "login_success.php"
-
}
-
else {
-
echo "Attenzione username o password errati";
-
}
-
?>
4. Creiamo il file login_success.php e inseriamo al suo interno il seguente codice:
-
// Controlla se la sessione è stata registrata, altrimenti rimanda alla pagina di login
-
// Questa prima parte dobbiamo inserirla in tutte le pagine che vogliamo proteggere con password prima di qualsiasi altra cosa
-
<?
-
}
-
?>
5. Infine creiamo il file logout.php che ci pemetterà di distruggere la sessione:
-
<?
-
?>
In questo modo abbiamo creato un semplice sistema di login in php + mysql
Post Correlati e Simili:
Ultimi 5 articoli di lordmarin
- Template Open source - January 27th, 2008
- css tutorial - Come definire la grandezza del font - April 3rd, 2007
- Css tutorial - Box con angoli arrotondati senza uso di immagini - April 3rd, 2007
- Apple supporto per vista con l'aggiornamento di Boot Camp - April 3rd, 2007
Add New Comment
Viewing 22 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.