Joskevic
Legacy Member
Ik probeer al een hele tijd om een werkend php/mysql loginscript te maken, maar steeds mislukt dit. Ik kan de gebruiker succesvol toevoegen, maar als ik dan probeer in te loggen krijg ik dat ik foute logininformatie ingeef. Dit is de code die ik momenteel heb. Kan iemand mij zeggen wat ik fout doe?
Login.php
Registratie.php
Login.php
PHP:
<?php
session_start();
$con = mysql_connect('localhost','***,'***') or die(mysql_error());
mysql_select_db('***',$con);
if ($_GET["op"] == "login")
{
if (!$_POST["username"] || !$_POST["password"])
{
die("U moet een gebruikersnaam en wachtwoord ingeven.");
}
// Create query
$q = "SELECT * FROM login "
."WHERE username='".$_POST['username']."' "
."AND password=PASSWORD('".$_POST['password']."') "
."LIMIT 1";
// Run query
$r = mysql_query($q);
if ( $obj = mysql_fetch_object($r) )
{
// Login good, create session variables
$_SESSION["valid_id"] = $obj->id;
$_SESSION["valid_user"] = $_POST["username"];
$_SESSION["valid_time"] = time();
// Redirect to member page
Header("Location: index.php?pagina=Leden");
}
else
{
// Login not successful
die("Gelieve de juiste login informatie in te geven aub.");
}
}
else
{
?>
<form action="?pagina=Login&op=login" method="POST">
<table>
<tr>
<td>Gebruikersnaam: </td>
<td><input name="username"></td>
</tr>
<tr>
<td>Wachtwoord: </td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
<?php
}
mysql_close($con);
?>
Registratie.php
PHP:
<?php
$con = mysql_connect('localhost','***','***') or die(mysql_error());
mysql_select_db('***',$con);
//Input vaildation and the dbase code
if ( $_GET["op"] == "reg" )
{
$bInputFlag = false;
foreach ( $_POST as $field )
{
if ($field == "")
{
$bInputFlag = false;
}
else
{
$bInputFlag = true;
}
}
// If we had problems with the input, exit with error
if ($bInputFlag == false)
{
die( "Er is een probleem opgetreden met uw ingevoerde gegevens. "
."Gelieve terug te keren en opnieuw te proberen.");
}
// Fields are clear, add user to database
// Setup query
$q = "INSERT INTO `login` (`username`,`password`,`email`) "
."VALUES ('".$_POST["username"]."', "
."PASSWORD('".$_POST["password"]."'), "
."'".$_POST["email"]."')";
// Run query
$r = mysql_query($q);
// Make sure query inserted user successfully
if ( !mysql_insert_id() )
{
die("Error: Gebruiker niet toegevoegd.");
}
else
{
// Redirect to thank you page.
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = "/index.php?pagina=Registratie&op=thanks";
echo ("<script>document.location.href='http://".$host.$uri.$extra."'</script>");
}
} // end if
//The thank you page
elseif ( $_GET["op"] == "thanks" )
{
echo "Bedankt voor uw registratie.";
}
//The web form for input ability
else {
?>
<form action="?pagina=Registratie&op=reg" method="POST">
<table>
<tr>
<td>Gebruikersnaam: </td>
<td><input name="username"></td>
</tr>
<tr>
<td>Wachtwoord: </td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td>E-mailadres: </td>
<td><input name="email" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Registreren"></td>
</tr>
</table>
</form>
<?php
}
mysql_close($con);
// EOF
?>