Jump to content

Archived

This topic is now archived and is closed to further replies.

whatnow

Simple Login Script with a problem

Recommended Posts

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,

[code]<?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();
?>[/code]


thanks in advance,

Share this post


Link to post
Share on other sites
hey

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

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

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

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!

Share this post


Link to post
Share on other sites
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:

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

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 :(

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
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

Share this post


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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.