Jump to content


Photo

Simple Login Script with a problem


  • Please log in to reply
5 replies to this topic

#1 whatnow

whatnow
  • Members
  • PipPip
  • Member
  • 12 posts
  • Locationuk

Posted 01 March 2006 - 08:51 PM

hi, I hope this isn't too noobish for even the noob's forum but i've only been on PHP for one day and a half, so please be gentle.

This is my login.php code. A simple form on login.html sends the data, i've checked with the Db and it inputs the data fine. It's reading the variables back out of the db that seems to be the issue. I've checked my cookie folder (testing on IE for now) and it'll create the cookie for loggedin but not mysite_username :(

this is my code processing the login,

<?php
ob_start();

include("config.php"); 

// connect to the mysql server 
$link = mysql_connect($server, $db_user, $db_pass) 
or die ("Could not connect to mysql because ".mysql_error()); 

// select the database 
mysql_select_db($database) 
or die ("Could not select database because ".mysql_error()); 

//select the username and password for a positive Ident.
$match = "select id from $table where username = '".$_POST['username']."' 
and password = '".$_POST['password']."';"; 

//query the match
$qry = mysql_query($match) 
or die ("Could not match data because ".mysql_error()); 
$num_rows = mysql_num_rows($qry); 

//if there's none, tell the user to try again.
if ($num_rows <= 0) { 
echo "Sorry, there is no username $username with the specified password.<br>"; 
echo "<a href=login.html>Try again</a>"; 
exit; 
} else { 

//set two cookies, one to say the user is logged in. 
setcookie("loggedin", "TRUE", time()+(3600 * 24));

//the second (not working) to set the username value to a cookie for later comsumption.
setcookie("mysite_username", "$username");

//retrieve the info previously saved...not working either.
$cookie_info = explode("-", $_COOKIE['mysite_username']);
$username = $cookie_info[0];
echo "hi: $username<br><br>";
echo "You are now logged in!<br>"; 
echo "Continue to the <a href=members.php>members</a> section."; 
}
ob_end_flush();
?>


thanks in advance,

#2 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 01 March 2006 - 09:54 PM

hey

this is a common mistake and easy to miss sometimes, but try changing

//the second (not working) to set the username value to a cookie for later comsumption.
setcookie("mysite_username", "$username");

to
//the second (not working) to set the username value to a cookie for later comsumption.
setcookie("mysite_username", $username);

when used as values, variables do not use quotes

more on cookies here:
[a href=\"http://www.phpfreaks.com/phpmanual/page/function.setcookie.html\" target=\"_blank\"]http://www.phpfreaks.com/phpmanual/page/fu....setcookie.html[/a]

Good luck!
got a problem? Google helps many of those in need

#3 whatnow

whatnow
  • Members
  • PipPip
  • Member
  • 12 posts
  • Locationuk

Posted 02 March 2006 - 08:20 AM

Hi, thanks for your reply, it makes sense what you mention and i've changed it.

The only problem is that i still can't get the variable $username out. If I log in with a incorrect password it'll stop the user and say

'Sorry, there is no username with the specified password.'

however my code is:

echo "Sorry, there is no username $username with the specified password.<br>";

It wont pump out $username under any echo or print. This in turn stops the cookie from being set as the value $username must be null...i've checked my I.E. cookies and it still wont make it.

I have no idea why though, it was working at one point and then I bodged it up :(

#4 whatnow

whatnow
  • Members
  • PipPip
  • Member
  • 12 posts
  • Locationuk

Posted 02 March 2006 - 08:41 AM

got it, made sense that I needed to add this:

$username = $_POST['username'];

but i'm not sure if i'm there yet, hehe, however I can now print the posted details :)

#5 earl_dc10

earl_dc10
  • Members
  • PipPipPip
  • Advanced Member
  • 71 posts

Posted 02 March 2006 - 09:21 PM

just remember to echo all the variables to make sure that there is something in them, that has saved me alot of time in the past

got a problem? Google helps many of those in need

#6 whatnow

whatnow
  • Members
  • PipPip
  • Member
  • 12 posts
  • Locationuk

Posted 02 March 2006 - 10:32 PM

thanks, yes i'm now doing that a lot just to check code in chunks...better to have visual errors :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users