Ciao, anche io mi stò avventurando nol mondo dei login, ti allego il link di html.it per la gestione degli utenti in aree riservate.
php.html.it
Inoltre ti posto il codice che stò utilizzando per fare esperimenti, magari ti torna utile (sempre trovato su google)
index.php
Codice PHP:
<html>
<head>
<title>Server2Go - Selfconfigurating WAMP Stack</title>
<link rel="stylesheet" href="format.css" type="text/css">
</head>
<body>
<table width="200" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checklogin.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3">[b]Member Login [/b]</td>
</tr>
<tr>
<td width="78">Username</td> <td width="6">:</td>
<td width="294"><input name="myusername" type="text" id="myusername"></td>
</tr>
<tr>
<td width="78">Password</td> <td width="6">:</td>
<td width="294"><input name="mypassword" type="password" id="mypassword"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
connect.php
Codice PHP:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="user"; // Database name
$tbl_name="members"; // Table name
?>
checklogin.php
Codice PHP:
<?php
include "connect.php";
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// 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"
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>
login_success.php
Codice PHP:
<?
session_start();
if(!session_is_registered(myusername)){
header("location:index.php");
}
?>
<html>
<body>
<form name="form2" method="post" action="logout.php">
Login Successful
<?php
echo "Benvenuto ".$_SESSION['myusername']." hai effettuato il login con successo
Puoi effettuare il logout";
?>
<input type="submit" name="Submit" value="Logout" action="logout.php">
</form>
</body>
</html>
logout.php
Codice PHP:
<?
session_start();
session_destroy();
header("location:index.php");
?>