Intanto ciao a tutti. Vi espongo il problema.
Ho uno script per il login in php. Ora vi posto i codici se puo essere utile a qualcuno:
connessione.php
codice:
<?php
$db_host = "localhost";
$db_user = " ";
$db_pass = " ";
$db_name = " ";
$db_conn = mysql_connect($db_host, $db_user, $db_pass) or die ('Errore connessione :' . mysql_error());
mysql_select_db($db_name, $db_conn);
?>
controllo.php
codice:
<?
include ('connessione.php');
session_start();
$logkey = $_SESSION['logkey'];
if (!isset($logkey)) {
echo 'Errore non sei autenticato! Effettua il login';
exit();
}
$query = "SELECT * FROM utenti WHERE logkey = '$logkey'";
$result = mysql_query($query, $db_conn) or die ('Errore query : ' . mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows != 1) {
echo 'Errore non sei autenticato! Effttua il login';
exit();
} else {
$row = mysql_fetch_assoc($result);
}
?>
Function_random.php
codice:
<?php
function SessioneCasuale(){
$N_Caratteri = 32;
$Stringa = "";
For($I=0;$I<$N_Caratteri;$I++){
do{
$N = Ceil(rand(48,122));
}while(!((($N >= 48) && ($N <= 57)) || (($N >= 65) && ($N <= 90)) || (($N >= 97) && ($N <= 122))));
$Stringa = $Stringa.Chr ($N);
}
return $Stringa;
}
?>
login.html
codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="script.php">
<table width="200" border="1" align="center">
<tr>
<td>Username</td>
<td><label>
<input type="text" name="username" id="username" />
</label></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" id="password" /></td>
</tr>
<tr>
<td></td>
<td>
<label>
<input type="submit" name="button" id="button" value="Invia" />
</label>
</td>
</tr>
</table>
</form>
</body>
</html>
privata.php
codice:
<?
include ('controllo.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body>
Benvenuto caro, <? echo $row['username']; ?>
</body>
</html>
registrazione.html
[code[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Documento senza titolo</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="script_iscriviti.php">
<table width="200" border="1" align="center">
<tr>
<td>Name</td>
<td><label>
<input type="text" name="name" id="name" />
</label></td>
</tr>
<tr>
<td>Surname</td>
<td><input type="text" name="surname" id="surname" /></td>
</tr>
<tr>
<td>Username</td>
<td><input type="text" name="username" id="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="text" name="password" id="password" /></td>
</tr>
<tr>
<td></td>
<td>
<label>
<input type="submit" name="button" id="button" value="Invia" />
</label>
</td>
</tr>
</table>
</form>
</body>
</html>
[/code]
script.php (quello che effettua il login)
codice:
<?php
include ('connessione.php');
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM utenti WHERE username = '$username' AND password = '$password'";
$result = mysql_query($query, $db_conn);
$num_rows = mysql_num_rows($result);
if ($num_rows != 1) {
echo 'Errore Useraname o Password non presenti sul database Effettua il login';
} else {
session_start();
include_once ('Function_random.php');
$logkey = SessioneCasuale();
$insert = "UPDATE utenti SET logkey = '$logkey' WHERE username = '$username'";
$res_insert = mysql_query($insert) or die('Errore query: ' . mysql_error());
$_SESSION['logkey'] = $logkey;
header ("Location: privata.php");
}
?>
script_iscriviti.php (colui che elabora i dati di registrazione)
codice:
<?php
// Includo i file per la connessione al DB
include ('connessione.php');
$name = $_POST['name'];
$surname = $_POST['surname'];
$username = $_POST['username'];
$password = $_POST['password'];
// Controllo sul database se è già presente un username uguale
$db_query = "SELECT * FROM utenti WHERE username = '$username'";
$rs = mysql_query($db_query, $db_conn) or die ('Errore query: ' . mysql_error());
$num_rows = mysql_num_rows($rs);
if ($num_rows != 0) {
echo 'Errore Username già presente sul database! Torna Indietro!'
exit();
}
// Inserisco i dati nel DB
$query = "INSERT INTO `utenti` (`id`, `name`, `surname`, `username`, `password`)
VALUES ('id', '$name', '$surname', '$username', '$password')";
$result = mysql_query($query, $db_conn) or die ('Errore query: ' . mysql_error());
mysql_close($db_conn);
header ("Location: login.php");
?>
tabella.sql
codice:
CREATE TABLE utenti (
id INT UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
surname VARCHAR(30) NOT NULL,
username VARCHAR(30) NOT NULL,
password CHAR(32) NOT NULL,
PRIMARY KEY(id),
INDEX(username, password)
);
Allora, hostato i files. Ho cominciato creando la tabella nal database ed inserendo i dati nel file connessione.php. Ho aperto registrazione.html ed ho compilato il form, dopodiche ho clikkato su invia.
E qui sta il problema, compare questo errore:
codice:
PHP Parse error: syntax error, unexpected T_EXIT, expecting ',' or ';' in C:\WebSites\pointpc.org_xfpxtrniyph8omi2\login\sistema_login\script_iscriviti.php on line 19
. La riga che indica l'errore (on line 19) è la seguente:
codice:
echo 'Errore Username già presente sul database! Torna Indietro!'
exit();
Sapete dirmi come risolvere? Grazie mille!