Login and Remember me function sessions and cookie problem?

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
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Login and Remember me function sessions and cookie problem?

Post by mudkicker »

Hi there, my website is http://www.teskere.com/mudkicker . I wrote a script that some can login with or without "remember me" function. but something is wrong there.. when someone is came for the firsttime user control panel is shown which is not possible if you're not logged... i send you the codes. please help me this is very important for me! thank you very much.

Code: Select all

<?php
 if(session_is_registered("ad") || $HTTP_COOKIE_VARS["loginhatirlat"]="logged") { 
?>
<table width="180" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="1" height="1" bgcolor="#0085BC"></td>
<td width="9" height="1" class="baslikmenu"></td>
<td height="1" class="baslikmenu">Merhaba  <?php
if(!$HTTP_SESSION_VARS["ad"]) { echo $HTTP_COOKIE_VARS["loginadi"]; } else { echo $HTTP_SESSION_VARS["ad"]; } 
?></td>
<td width="1" height="1" bgcolor="#0085BC"></td>
</tr>
<tr>
<td width="1" bgcolor="#0085BC"></td>
<td width="9">&nbsp;</td>
<td><a href="profil.php" class="normal">Kullan&#305;c&#305; Bilgilerini De&#287;i&#351;tir</a> </td>
<td width="1" bgcolor="#0085BC"></td>
</tr>
<tr>
<td bgcolor="#0085BC"></td>
<td width="9">&nbsp;</td>
<td class="normal"><a href="logout.php" class="normal">Sistemden Ç&#305;k </a></td>
<td bgcolor="#0085BC"></td>
</tr>
<tr bgcolor="#0085BC">
<td width="1" height="1"></td>
<td width="9" height="1"></td>
<td height="1"></td>
<td width="1" height="1"></td>
</tr>
</table>          
<?php
 } else { 
?><form action="log.php" method="post">
<table width="180" border="0" cellspacing="0" cellpadding="0">
<tr bgcolor="#0085BC">
<td width="1" height="1"></td>
<td height="1"></td>
<td height="1" colspan="2"></td>
<td width="1" height="1"></td>
</tr>
<tr>
<td width="1" bgcolor="#0085BC"></td>
<td width="9" class="baslikmenu"></td>
<td colspan="2" class="baslikmenu">Login</td>
<td width="1" bgcolor="#0085BC"></td>
</tr>
<tr>
<td bgcolor="#0085BC"></td>
<td width="9" class="normal"></td>
<td width="60" class="normal"><div align="right"><strong>K. Ad&#305;:</strong></div></td>
<td width="109" class="normal">
<center>
<input name="user" type="text" class="button" id="user" size="10">
</center></td>
<td bgcolor="#0085BC"></td>
</tr>
<tr>
<td width="1" bgcolor="#0085BC"></td>
<td width="9" class="normal"></td>
<td width="60" class="normal"><div align="right"><strong>&#350;ifre:</strong></div></td>
<td width="109" class="normal">
<center>
<input name="pass" type="password" class="button" id="pass" size="10">
</center></td>
<td width="1" bgcolor="#0085BC"></td>
</tr>
<tr>
<td bgcolor="#0085BC"></td>
<td class="normal"></td>
<td width="60" class="normal">
<div align="right">
<input name="hatirla" type="checkbox" id="hatirla" value="ok">
</div></td>
<td width="109" class="normal">Beni Hat&#305;rla! </td>
<td bgcolor="#0085BC"></td>
</tr>
<tr>
<td bgcolor="#0085BC"></td>
<td class="normal"></td>
<td width="60" class="normal">&nbsp;</td>
<td width="109" class="normal">
<input name="loginsend" type="submit" class="button" id="loginsend" value="Gönder!">
<input name="logsil" type="reset" class="button" value="Sil!">
</td>
<td bgcolor="#0085BC"></td>
</tr>
<tr>
<td width="1" bgcolor="#0085BC"></td>
<td height="1" bgcolor="#0085BC"></td>
<td height="1" colspan="2" bgcolor="#0085BC"></td>
</tr>
</table>
</form>
<?php
}
?>
This part of the page code show you if you are logged the user control panel or login form!

Code: Select all

<?php
session_start();
if (!isset($_POST[user]) || !isset($_POST[pass]) || empty($_POST[user]) || empty($_POST[pass]))
{
header("Location: oops.php?action=errorlogin");
}
else
{
include("db.php");
$db = new Db;
	$query = mysql_query("SELECT * FROM uyeler WHERE kulad='".$_POST[user]."' AND kulsif='".md5($_POST[pass])."'");
	$row = mysql_fetch_array($query);
	$num = mysql_num_rows($query);
	if ($num > 0)
	{
	session_register("ad");
	$ad = $row["ad"];
		if(isset($hatirla))
		{
		setcookie ("loginhatirlat","logged",time()+86400);
		setcookie ("loginadi",$ad,time()+86400);
		}
	header("Location: index.php");
	}
	else
	{
	header("Location: oops.php?action=errorlogin");
	}
$db->Close();
}
?>
THis one is the script which logs you and authenticates you.

Code: Select all

<?php

if($HTTP_COOKIE_VARS["loginhatirlat"]="logged")
{
setcookie ("loginhatirlat","notlogged",time()+86400);
session_destroy();
?>
Sistemden ba&#351;ar&#305;l&#305; bir &#351;ekilde ç&#305;kt&#305;n&#305;z. Anasayfaya dönmek için <a href="index.php" class="baslik">t&#305;klay&#305;n</a>.<br>
Pencereyi kapatmak için <a href="javascript:window.close();" class="baslik">t&#305;klay&#305;n</a>.

<?php
}
if(session_is_registered("ad"))
{
setcookie ("loginhatirlat","notlogged",time()+86400);
session_unset();
session_destroy();
?>
Sistemden ba&#351;ar&#305;l&#305; bir &#351;ekilde ç&#305;kt&#305;n&#305;z. Anasayfaya dönmek için <a href="index.php" class="baslik">t&#305;klay&#305;n</a>.<br>
Pencereyi kapatmak için <a href="javascript:window.close();" class="baslik">t&#305;klay&#305;n</a>.
<?php
}
else
{
header( "Location : index.php" );
}
?>
And this one is the code to log out...

Have a nice day, i hope someone can find the problem here, thanks!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

First glance at the code makes me think that you are automaticly setting a variable to "logged" (you are missing an additional =).

Code: Select all

// you have:
$HTTP_COOKIE_VARS["loginhatirlat"]="logged") { 
// wouldn't that be:
$HTTP_COOKIE_VARS["loginhatirlat"]=="logged") {
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

thanks jam. let's have a look if it's solved...
i'll write here later.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Welcome, and please do. Nice to store solutions for other readers.
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

Post by mudkicker »

i think it's all right. but if you can test it it will be very appreciated!!! btw thanks jam one character makes the life different, huh? ;)
Post Reply