Page 1 of 1

PHP log in form

Posted: Sat Jan 03, 2004 4:24 am
by Persone
Hi everyone,

I have a problem with my php. I have log-in form, like you can see,and i want that php set a cookie but in this code he doesn't set a cookie.
After the user logged in, php check if there is a cookie and if there is he goes to cp.php. My problem is that in this code he doesn't set a cookie. What's wrong with this code. Plz help me

Persone

Code: Select all

<? 
if (isset($_POST['login'])){ 
if (($_POST['gebruikersnaam'] != "") && ($_POST['wachtwoord'] != "")){ 
require_once ("vars.php"); 
$Verbinding = @mysql_connect($Host, $Gebruiker, $WachtWoord) OR DIE ("Kon geen verbinding opnemen met de database!\n"); 
$controleren = "SELECT * FROM login WHERE gebruiker = '" . $_POST['gebruikersnaam'] . "' AND wachtwoord = '" . $_POST['wachtwoord'] . "'"; 
$controle = @mysql_db_query($DBNaam, $controleren, $Verbinding) OR DIE ("<b>Kon de query niet uitvoeren door volgende fout:</b><br />". mysql_errno() ." : " . mysql_error()); 
$bestaat_gebruiker = @mysql_num_rows($controle); 
if ($bestaat_gebruiker == "1"){ 
$gegevens = @mysql_fetch_object($controle); 
setcookie("gebruiker", $gegevens->gebruiker, time() + "31536000"); 
setcookie("paswoord", $gegevens->wachtwoord, time() + "31536000");
$druk_meta = "<meta http-equiv="Refresh" content="4; URL=cp.php">\n";
$druk_af = "U bent succesvol ingelogd!\n"; 
}else{ 
$druk_af = "Sorry, maar u hebt een foutieve gebruiker/paswoord ingevoerd!\n"; 
$druk_meta = "";
} 
}else{ 
$druk_af = "U hebt geen gegevens ingevuld!\n"; 
$druk_meta = "";
} 
}else{ 
$druk_meta = "";
$druk_af = "<form name="loginform" action="" . $_SERVER['PHP_SELF'] . "" method="post">\n"; 
$druk_af .= "<table border="0" cellpadding="1" cellspacing="1">\n"; 
$druk_af .= "<tr><td class="style7">Gebruikersnaam:</td><td><input type="text" name="gebruikersnaam" id="gebruikersnaam" maxlength="32" size="20" /></td></tr>\n"; 
$druk_af .= "<tr><td class="style7">Wachtwoord:</td><td><input type="password" name="wachtwoord" id="wachtwoord" maxlength="32" size="20" /></td></tr>\n"; 
$druk_af .= "<input type="hidden" name="login" value="login" />\n"; 
$druk_af .= "<tr><td colspan="2" align="middle"><input type="submit" value="login" /></td></tr>\n"; 
$druk_af .= "</table>\n"; 
$druk_af .= "</form>\n"; 
} 
?> 
<html> 
<head> 
<title> 
:: Login :: 
</title> 
<style type="text/css"> 
.style6 { 
FONT-WEIGHT: bold; 
FONT-SIZE: 18px; 
COLOR: #008000; 
FONT-FAMILY: "Century Schoolbook"; 
} 
BODY { 
MARGIN-LEFT: 20px; 
} 
.style7 { 
FONT-SIZE: 15px; 
FONT-FAMILY: "Century Schoolbook"; 
} 
</STYLE> 
<?
echo $druk_meta;
?>
</HEAD> 
<BODY> 
<? 
echo $druk_af; 
?> 
</body> 
</html>

Posted: Fri Jan 16, 2004 7:48 am
by kendall
Persone,

not very good at this... i myself dont understand how this whole setcookie thing work but the following should be considered

Maybe you cant setcookies....broswers to day can be set to not accept cookies so you may want to check on that better you do a session id thing and let that expire

check your time against the tim on the server....depending on where you are and where your server is the time zone maybe different and this can affect the setcookie i had a problem like that where my server is in one country and i am in a next

uhmmm...im trying to look at your code...i dont see anything else that cud go wrong

Kendall

Posted: Fri Jan 16, 2004 8:13 am
by patrikG
On a general note: when a page sets a cookie, it can only be read after/when the page reloads.

How does cp.php check if a cookie is set? Can you post the code please?
If you're using $_COOKIE there I don't see a reason why it shouldn't work.

Regarding your code: I would seriously consider reformating it, as the structure of your conditions (if ... else) is very confusing.

Posted: Fri Jan 16, 2004 8:44 am
by kettle_drum
Check to see that your cookie is setting corectly by making sure the cookie is in your "cookie directory" after you submit the form - or turn up privacy ratings so it warns you when sites try to send you cookies.