PHP log in form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Persone
Forum Newbie
Posts: 1
Joined: Sat Jan 03, 2004 4:24 am

PHP log in form

Post 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>
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Post 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
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post 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.
Post Reply