Salve, sono nuovo e sto realizzando uno script che permette di registrarsi al sito.
E composto da diversi files:
install.php
Codice PHP:
<?php
include ("config.php");
//creating tabe
mysql_query("CREATE TABLE log_user (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY, username VARCHAR(60), password VARCHAR(60), email VARCHAR(60), account VARCHAR(60))");
Print "<h3>Your table has been created<h3>";
?>
Config.php
Codice PHP:
<?php
// Data of your Database
$host = "localhost";
$user = "user";
$pass = "pass";
$nome = "my_table";
// Connection to Database
mysql_connect("$host", "$user", "$pass") or die(mysql_error());
mysql_select_db("$nome") or die(mysql_error());
?>
login.php
Codice PHP:
<?php
include ("config.php");
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM log_user WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: members.php");
}
}
}
//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted
// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('Completare TUTTI i campi, Grazie.');
}
// checks it against the database
if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM log_user WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die("L'utente ".$_POST['username']." non esiste. <a href=register.php>Clicca Qui per Registrarti</a>");
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die("La password e' incorretta, riprova!");
}
else{
// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
//then redirect them to the members area
header("Location: page.php");
}
}
}
else
{
// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Login</h1></td></tr>
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="40">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="50">
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Login">
</td></tr>
</table>
</form>
<tr><td><a href="register.php">regiser</td><td>
<?php
}
?>
logout.php
Codice PHP:
<?php
$past = time() - 100;
//this makes the time in the past to destroy the cookie
setcookie(ID_my_site, gone, $past);
setcookie(Key_my_site, gone, $past);
header("Location: login.php");
?>
members.php (pagine che viene vista solo se si è loggati)
Codice PHP:
<?php
// Connects to your Database
include ("config.php");
//checks cookies to make sure they are logged in
if(isset($_COOKIE['ID_my_site']))
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM log_user WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
//if the cookie has the wrong password, they are taken to the login page
if ($pass != $info['password'])
{ header("Location: login.php");
}
//otherwise they are shown the admin area
else
{
echo "Admin Area<p>";
echo "Your Content<p>";
echo "<a href=logout.php>Logout</a>";
}
}
}
else
//if the cookie does not exist, they are taken to the login screen
{
header("Location: login.php");
}
?>
Register.php
Codice PHP:
<?php
include ("config.php");
//This code runs if the form has been submitted
if (isset($_POST['submit'])) {
//This makes sure they did not leave any fields blank
if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2'] | !$_POST['mail'] ) {
die('Completare TUTTI i campi, Grazie.');
}
// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['username'] = addslashes($_POST['username']);
}
$usercheck = $_POST['username'];
$check = mysql_query("SELECT username FROM log_user WHERE username = '$usercheck'")
or die(mysql_error());
$check2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($check2 != 0) {
die(" ATTENZIONE, L' username ".$_POST['username']." e' gia'* in uso ");
}
// checks if the mail is in use
if (!get_magic_quotes_gpc()) {
$_POST['mail'] = addslashes($_POST['mail']);
}
$mailcheck = $_POST['mail'];
$check = mysql_query("SELECT email FROM log_user WHERE email = '$mailcheck'")
or die(mysql_error());
$mailcheck2 = mysql_num_rows($check);
//if the name exists it gives an error
if ($mailcheck2 != 0) {
die("Attenzione, L' Email ".$_POST['mail']." e' gia' in uso.");
}
// checks if the mail correct.
if(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$_POST['mail'] )){
die("Attenzione, L' Email ".$_POST['mail']." e' incorretta.");}
// this makes sure both passwords entered match
if ($_POST['pass'] != $_POST['pass2']) {
die('Le password non corrispondono. ');
}
// here we encrypt the password and add slashes if needed
$pass = $_POST['pass'];
$_POST['pass'] = md5($_POST['pass']);
if (!get_magic_quotes_gpc()) {
$_POST['pass'] = addslashes($_POST['pass']);
$_POST['username'] = addslashes($_POST['username']);
}
// now we insert it into the database
$token = "testo a caso";
$mailcr = md5($_POST['email'].$token);
$insert = "INSERT INTO log_user (username, password, email, account)
VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['mail']."', '".$mailcr."')";
$add_member = mysql_query($insert);
?>
<h1>Registrato con Successo!</h1>
<p>Grazie, si e' correttamente registrato, Confermi la registrazione con l' email che le e' stata inviata.</a>.</p>
<?php
}
else
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Username:</td><td>
<input type="text" name="username" maxlength="60">
</td></tr>
<tr><td>Password:</td><td>
<input type="password" name="pass" maxlength="10">
</td></tr>
<tr><td>Conferma Password:</td><td>
<input type="password" name="pass2" maxlength="10">
</td></tr>
<tr><td>Email:</td><td>
<input type="text" name="mail" maxlength="60">
</td></tr>
<tr><th colspan=2><input type="submit" name="submit" value="Registrati"></th></tr> </table>
</form>
<?php
}
?>
Ora però vorrei inserire una email che permetta di attivare l'account.
Ho inserito questo codice dopo la creazione del utente nel file register.php:
Codice PHP:
// email per la conferma
// intestazioni
$date = time('dmy');
$headers = "From: your email";
$subject = "Conferma la tua iscrizione.";
//corpo del messaggio
$messaggio = "Ti ringraziamo per la tua iscrizione.\n";
$messaggio .= "La tua user è: ".$_POST['username']."\n";
$messaggio .= "La tua password è: ".$pass."\n";
$messaggio .= 'Per confemare vai alla pagina http://your site.altervista.org/log_in/activation.php?user='.$mailcr.'&date='.$date.'';
// invio dell'email
@mail($_POST['mail'], stripslashes($subject),stripslashes($messaggio),$headers);
Ora però non so come creare uno script che cambia il campo "account": dalla email criptata criptata al valore "1"
Ho trovato questo ma non funziona:
Codice PHP:
$query = "UPDATE log_user SET account=1 WHERE md5(concat(email,$token))=".$_GET['user'];
$enable_member = mysql_query($query);
Quindi ho deciso di chiedere assistenza qui!
Ringrazio anticipatamente a chi mi darà un aiuto!!!