Jump to content


'setcookie' problem.


3 replies to this topic

#1 outofexile

    Young Padawan

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Location:UK

Posted 20 March 2008 - 09:01 PM

I've come across a bug in my custom login system (i've coded it myself), and well it worked for a small while, however all of a sudden, one of the cookies that I use to keep people logged in isn't being set.

The weird thing is, it works for my friend and it works when I use the form on my forum's pages (its on the same hosting etc.)

$check = mysql_query("SELECT * FROM user_info WHERE user='$user'");

while ($row = mysql_fetch_array($check))
	{
$username = $row['user'];
	}

$checkpass = mysql_query("SELECT * FROM user_info WHERE pass='$encpass'");
	
while ($row = mysql_fetch_array($checkpass))
	{
$password = $row['pass'];
	}
	
if (isset ($usercook) && isset($enccook))
	{
	include ('sidebar.php');
	}
else
	{
if (!isset ($user))
	{
	writeForm();
	}
else
	{
if ($user == $username)
	{
	if ($encpass == $password)
		{
		if (empty($user) || empty($pass)) 
			{
			writeForm();
			die(''); 
			}
		include ('sidebar.php');
		print "";
		setcookie("pass", "$pass");  //gives user cookie with submitted password
		setcookie("user", $user, time()+3600);  /* expire in 1 hour */
		
		}
	else
		{
		print ("");
		writeForm();
		}
	}
else
	{
	print ("");
	writeForm();
	}
	}
	}\
	
print ("");

print ("");
print ("[url="http:///index.php"]Home[/url]

");
print ("");
print ("[url="http:///about.php"]About[/url]

");
print ("");
print ("[url="http:///portfolio.php"]Portfolio[/url]

");
print ("");
print ("[url="http://forum.php"]Forum[/url]

");
print ("");
print ("[url="http:///contact.php"]Contact[/url]

");
print ("");
print ("[url="http:///affiliates.php"]Affiliates[/url]

");
print ("");

include ('con_log.php');

$usercook = $_COOKIE["user"];
$passcook = $_COOKIE["user"];
$enccook = md5($passcook);
$user = $_POST["user"];
$pass = $_POST["pass"]; //cookie is gathered
$encpass = md5($pass);

function writeForm ()
	{
	print ("");
	print ("Username:



 Password:



 

 [url="http://register.php"]Register[/url]

Please enter your username and password.

");
		print ("ERROR: Incorrect password.

");
		print ("");
		print ("");
	print ("ERROR: Incorrect username.

");
	print ("");
	print ("");



?>

The 'user' cookie never gets set. I have done a number of tests, and the $user variable does get set, but the cookie doesn't

Any ideas as to what the problem could be?

If you need/want any more information, i'll do my best to provide it.

Edited by outofexile, 21 March 2008 - 06:37 AM.


#2 dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 21 March 2008 - 02:08 AM

Cookies should be set in your header, so before your print Anything (or include..)

Can I ask you, what's with all the:
print "";

Your code is a bit messy aswell, might want to try something like this:
<?php
session_start();

//......
//previous code
//......


$qry= mysql_query("SELECT * FROM user_info WHERE user='" . $user . "'");
if(mysql_num_rows($qry) == 1){
	$userinfo = mysql_fetch_array($qry);
	if($userinfo["pass"] == $encpass){
		//credentials are correct
		$sessionID = md5($user . $time);
		setcookie("session", $sessionID, time()+3600);
		$_SESSION["session"] == $sessionID;
		$_SESSION["user"] == $user;
		include ('sidebar.php');
	}		
}
?>
NEVER EVER put username and password in cookies since they can be tracked, sniffed, logged, .... Always try and use a unique sessionID value wich you could link to the user in either $_SESSION or your database!

#3 outofexile

    Young Padawan

  • Members
  • Pip
  • 7 posts
  • Gender:Male
  • Location:UK

Posted 21 March 2008 - 06:33 AM

I'm really not sure about why the print functions are there :S I'll tidy it up now.

Thanks for the info, but with that, on other pages, how do I protect the content, would it be
$qry= mysql_query("SELECT * FROM user_info WHERE user='" . $user . "'");
if(mysql_num_rows($qry) == 1){
	$userinfo = mysql_fetch_array($qry);
	if($userinfo["pass"] == $encpass){
		//credentials are correct
		$sessionID = md5($user . $time);
		setcookie("session", $sessionID, time()+3600);
		$_SESSION["session"] == $sessionID;
		$_SESSION["user"] == $user;
		//CONTENT HERE
	}		
}

on every page that needs protecting??

And to log out,
setcookie("session", $sessionID, time()-3600);
?

Edited by outofexile, 21 March 2008 - 06:39 AM.


#4 dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 21 March 2008 - 06:44 AM

At first, I'd like to correct an error I made in my previous post:
<?php
session_start();

//......
//previous code
//......


$qry= mysql_query("SELECT * FROM user_info WHERE user='" . $user . "'");
if(mysql_num_rows($qry) == 1){
	$userinfo = mysql_fetch_array($qry);
	if($userinfo["pass"] == $encpass){
		//credentials are correct
		$sessionID = md5($user . $time);
		setcookie("session", $sessionID, time()+3600);
		$_SESSION["session"] = $sessionID;
		$_SESSION["user"] = $user;
		include ('sidebar.php');
[b]		//You are logged in from here[/b]
	}		
}
?>
I put double == in assigning the $_SESSION, sry about that :-)

To protect your other pages you could do something like this:

if($_COOKIE["session] == $_SESSION["session"]){
	$qry= mysql_query(\"SELECT * FROM user_info WHERE user='" . $_SESSION["user"] . "'");
	if(mysql_num_rows($qry) == 1){
		//You can assume the user is logged in place other content here
	}
}

Logging out could be something like the following:

function logOut(){
	$_SESSION = array();
	setcookie("sessionid",NULL,time()-1800);
		header("Location: index.php"); //Remeber to not print any text before this, since it's header information like cookies!
}

If you want a detailed guide on how to make a secure login system with databases and everything, reply to this message or PM me, I'll be glad to help you out!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users