activeringscode

Status
Niet open voor verdere reacties.

savant11

Gebruiker
Lid geworden
7 jan 2008
Berichten
153
Hoi,

heb het volgende script:

Code:
<?php 
include("connection.php"); 

session_start();

if(isset($_POST['submit']))
{

$voornaam = addslashes(trim($_POST['voornaam']));
$tussenvoegsel = addslashes(trim($_POST['tussenvoegsel']));
$achternaam = addslashes(trim($_POST['achternaam']));
$geboortedatum =  addslashes(trim($_POST['geboortedatum']));
$postcode =  addslashes(trim($_POST['postcode']));
$land =  addslashes(trim($_POST['land']));
$username = addslashes(trim($_POST['username']));
$email = addslashes(trim($_POST['email']));
$pass = addslashes(trim($_POST['password']));
$conf = addslashes(trim($_POST['confirm']));

$ip = $_SERVER['REMOTE_ADDR'];
$date = date("d, m y");

if ( $_POST['password'] == $_POST['confirm'] )
{}else{

echo '<script>alert("Your passwords were not the same, please enter the same password in each field.");</script>';
echo '<script>history.back(1);</script>';
exit;

}

$password = md5($pass);

if ((((( empty($voornaam) ) || ( empty($achternaam) ) || ( empty($username) ) || ( empty($email) ) || ( empty($password) )))))
{

echo '<script>alert("One or more fields was left empty, please try again.");</script>';
echo '<script>history.back(1);</script>';
exit;

}

if((!strstr($email , "@")) || (!strstr($email , ".")))
{

echo '<script>alert("You entered an invalid email address. Please try again.");</script>';
echo '<script>history.back(1);</script>';
exit;

}

$q = mysql_query("SELECT * FROM gebruikers WHERE username = '$username'") or die(mysql_error());
if(mysql_num_rows($q) > 0)
{

echo '<script>alert("The username you entered is already in use, please try again.");</script>';
echo '<script>history.back(1);</script>';
exit;

}

$name = $voornaam . ' ' . $achternaam;
//$actkey = mt_rand(1, 500).'f78dj899dd';
//$act = sha1($actkey);


//******************BEGIN*************************************************************
//  Hier wordt de unieke sleutel gecreeerd voor de mail!!
// 
function randomstring($len) 
{ 
   srand(date("s")); 
   while($i<$len) 
     { 
       $str.=chr((rand()%26)+97); 
       $i++; 
   } 
  
   $str=$str.substr(uniqid (""),0,22); 
   return $str; 
} 

// Now generate the 6-digit code 
$actkey = randomstring(6);    // 6 for a 6-digit code 
$actkey = substr("$actkey", 0, 6);    // added to fix bug. 
//***************EINDE*************************************************************************




$query = mysql_query("INSERT INTO gebruikers (username, password, voornaam,achternaam,tussenvoegsel,geboortedatum,
postcode,land,geslacht, email,ip,actkey,active) 
VALUES ('$username','$password','$voornaam','$achternaam','$tussenvoegsel','$geboortedatum'
,'$postcode','$land','$geslacht','$email','$ip','$actkey','$active')") or die(mysql_error());

$send = mail($email , "Registration Confirmation" , "Thank you for registering by Coinnovation!.\n\n
Your username and password is below, along with details on how to activate your account.\n\nUser: 
".$username."\nPass: ".$pass."\n\nClick the link below to activate your account:
\nhttp://localhost/www/hsleiden/project2_2007/php/login2/activate.php?id=".$actkey."     \n\n
Please do not reply, this is an automated mailer.\n\nThanks", "FROM: [email]nengelen@orange.nl[/email]");

if(($query)&&($send))
{

echo ' <html>
<head>
<title>Success</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="success">
<p>Thank you for registering, you will recieve an email 
soon with your login details and your activation link so that you can activate your account.</p>
<p><a href="login.php">Click here</a> to login once you have activated.</p>
</div>

</body>
</html>
'; 

} else {

echo '
<html>
<head>
<title>Error</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="error">
<p>We are sorry, there appears to be a problem with our script at the moment.</p>
<p>Your data was not lost. Username: '.$username.' | Password: '.$pass.' | Email: '.$email.' | Full name: '.$name.'</p>
<p>Please try again later.</p>
</div>

</body>
</html>
'; 

}

} else {



mysql_close(); 


} 







?> 


<html>
<head>
<title>Register</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>

<body>

<div id="wrapper">

<div id="head">Registratie pagina</div>
<br>
<div id="main"> 
<p>Je kan je hier geristreren! </p>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="50%">Voornaam </td>
<td width="50%"><input name="voornaam" type="text" id="voornaam"></td>
</tr>
<tr>
<td width="50%">Tussenvoegsel </td>
<td width="50%"><input name="tussenvoegsel" type="text" id="tussenvoegsel"></td>
</tr>

<tr>
<td>Achternaam</td>
<td><input name="achternaam" type="text" id="achternaam"></td>
</tr>
<tr>
<td width="50%">Geboortedatum </td>
<td width="50%"><input name="geboortedatum" type="text" id="geboortedaum"></td>
</tr>
<tr>
<td width="50%">Land </td>
<td width="50%"><input name="land" type="text" id="land"></td>
</tr>
<tr>
<td width="50%">Postcode </td>
<td width="50%"><input name="postcode" type="text" id="postcode"></td>
</tr>

<tr>
<td width="50%">Geslacht </td>
<td width="50%"><input name="geslacht" type="radio" id="man"  value="man" checked="checked"></td>
<tr><td></td><td><input name="geslacht" type="radio" id="vrouw" value="vrouw" checked="checked"></td></tr> 


</tr>

<tr>
<td>Email Address </td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td>gebruikers naam</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td>Password(herhaal)  </td>
<td><input name="confirm" type="password" id="confirm"></td>
</tr>
<tr>
<td>Register</td>
<td><input name="submit" type="submit" class="textBox" value="Submit"></td>
</tr>
</table>
</form>
deze website. </div>

</div>

</body>
</html>


 

Hier dan nog het script "activatie.php" :

<?php

include("connection.php"); 

session_start();
// Eerst een basiscontrole uitvoeren op de URL
if(!isset($_GET['sleutel']) || !isset($_GET['username'])) {
    echo 'De URL is ongeldig.';
} else {
    // Om een account te activeren moeten we gewoon de activatie-
    // gegevens uit de tabel gooien:
    mysql_query("DELETE FROM gebruikers WHERE
                            gebruiker_id = ".intval($_GET['gebruiker'])." AND
                            sleutel       = '".mysql_real_escape_string($_GET['sleutel'])."'");

    //  mysql_affected_rows() 
    // of in dit geval dus verwijderd. Zo weten we ook gelijk of de gegevens wel juist waren!
    if(mysql_affected_rows() == 0) {
        echo 'De account bestaat niet of was reeds geactiveerd.';
    } else {
        // Er is een rij aangetast, dus is het gelukt :)
        echo 'De activatie is gelukt. Je kan nu <a href="login.php">Inloggen</a>';
    }
}
?>

Maar kryg dus wel een email met de link waar je op moet klikken om registratie te bevestigen, maar als ik dan op de link klik, kryg ik elke x de boodschap:

"De URL is ongeldig"!!

Een voorbeeld van zo links is:

http://localhost/www/hsleiden/project2_2007/php/login2/activate.php?id=ydazgn

Greetings
 
Laatst bewerkt:
Leuk scipt maar wat bedoel je er mee??

Je moet even iets meer info geven.. zo hebben wij er geen idee van wat je vraag is..Want er zit geen vraag in...Toch????
 
Leuk scipt maar wat bedoel je er mee??

Je moet even iets meer info geven.. zo hebben wij er geen idee van wat je vraag is..Want er zit geen vraag in...Toch????

hoi,

ja dit zit er nog in:

Maar kryg dus wel een email met de link waar je op moet klikken om registratie te bevestigen, maar als ik dan op de link klik, kryg ik elke x de boodschap:

"De URL is ongeldig"!!

Maar deze melding zit in het script , sorry!:shocked:
 
Stel voortaan wat duidelijker je vraag en plaats je PHP-code tussen [.PHP] en [./PHP].

Hier zit de fout:

PHP:
$send = mail($email , "Registration Confirmation" , "Thank you for registering by Coinnovation!.\n\n
Your username and password is below, along with details on how to activate your account.\n\nUser: 
".$username."\nPass: ".$pass."\n\nClick the link below to activate your account:
\nhttp://localhost/www/hsleiden/project2_2007/php/login2/activate.php?id=".$actkey."     \n\n
Please do not reply, this is an automated mailer.\n\nThanks", "FROM: ");

Je laat de geregistreerde klikken op de link 'http://localhost/www/hsleiden/project2_2007/php/login2/activate.php?id=".$actkey.'.

Vervolgens doe je dit:

PHP:
if(!isset($_GET['sleutel']) || !isset($_GET['username'])) {
    echo 'De URL is ongeldig.';
} else {
    // Om een account te activeren moeten we gewoon de activatie-
    // gegevens uit de tabel gooien:
    mysql_query("DELETE FROM gebruikers WHERE
                            gebruiker_id = ".intval($_GET['gebruiker'])." AND
                            sleutel       = '".mysql_real_escape_string($_GET['sleutel'])."'");

    //  mysql_affected_rows() 
    // of in dit geval dus verwijderd. Zo weten we ook gelijk of de gegevens wel juist waren!
    if(mysql_affected_rows() == 0) {
        echo 'De account bestaat niet of was reeds geactiveerd.';
    } else {
        // Er is een rij aangetast, dus is het gelukt :)
        echo 'De activatie is gelukt. Je kan nu <a href="login.php">Inloggen</a>';
    }
}

Maar daar klopt geen **** van. Je vraagt namelijk om variabelen die helemaal niet in de URL zijn meegegeven, vergelijk...

PHP:
http://localhost/www/hsleiden/project2_2007/php/login2/activate.php?id=".$actkey.
met
PHP:
if(!isset($_GET['sleutel']) || !isset($_GET['username'])) {

Je haalt dus twee dingen door elkaar. Ik heb zelf een soortgelijk activatiescript liggen, die kun je dan beter gebruiken.

Functie om een waarde een code te genereren.
PHP:
function generateHash ( $length )
    {
        
		$chars    = 'abcdefghijklmnopqrstuvwxyz23456789';
        $numChars = strlen ( $chars );
 
        $string = '';
		
        for ( $i = 0; $i < $length; $i++ ) 
		{
            
			$string .= substr ( $chars, rand ( 1, $numChars ) - 1, 1 );
			
        }
		
        return sha1($string);
		
    }

Vervolgens het registratiescript:

PHP:
	$code = generateHash(10);
	// De code genereren.

		$insertact = "INSERT INTO activation (id_user, sha1) VALUES (".$id.", '".$code."')";
// De net-geregistreerde user met z'n code in de tabel 'activation' zetten.

$actresult = mysql_query($insertact);	
						
$titel = "Activatiecode";
$boodschap =
"Beste blabla,

Uw activatielink.

http://www.url.nl/activation.php?id=$id&activation=$code&type=login_activation
";
												
$ok = mail($mail, $titel, $boodschap, "FROM: Bla <bla@bla.nl>");

In de link staat de id van de user en de code. Vervolgens komen we bij activation.php.

PHP:
$id = $_GET['id'];
$code = $_GET['activation'];
$type = $_GET['type'];

if($type = 'login_activation'){

	$query = "SELECT * FROM activation WHERE sha1 = '".$code."' AND id_user = '".$id."'";
	
		$result = mysql_query($query);
			$numrows = mysql_num_rows($result);
	
	if($numrows == 1){
	
		$update = "UPDATE users SET activation = 1 WHERE id_user = '".$id."'";
			$resultupdate = mysql_query($update);
			
		$delete = "DELETE FROM activation WHERE id_user = '".$id."'";
			$resultdelete = mysql_query($delete);
		
		echo $l_activationMessage;
		
	}else{
	
		echo $l_deactivationMessage;
		
	}
	
}else{

	

}

En bovenstaande code zou je zelf uit moeten kunnen vogelen.


Succes!
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan