Jump to content

Simple Login Script with a problem


whatnow

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,
Link to comment
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!
Link to comment
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 :(
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.