login php 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
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

login php problem

Post by mellowman »

hey guys ive ben workin on a php script so that i can login and have users on my site but my registration works but my login page gives me this error

Code: Select all

Parse error: parse error, unexpected T_STRING in web4designzsite/login/login.php on line 31
and this is the code that is in the php login file...

Code: Select all

<?php
 
//Database Information
 
$dbhost = "----------";
$dbname = "----------";
$dbuser = "----------";
$dbpass = "----------";
 
 
//Connect to database
 
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
 
session_start();
$username = $_POST[‘username’];
$password = md5($_POST[‘password’]);
 
<------this is line 31---->$query = “select * from users where username=’$username’ and password=’$password’”;
 
$result = mysql_query($query);
 
if (mysql_num_rows($result) != 1) {
$error = “Bad Login”;
    include “login.html”;
 
} else {
    $_SESSION[‘username’] = “$username”;
    include “memberspage.php”;
}
 
?>
 
any help ?
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: login php problem

Post by califdon »

I don't see any syntax errors, but have you taken a look at the values you're passing into the query string? Try echoing them, or echo your entire query string right after you assign it.

But DO protect yourself from hackers; use mysql_real_escape_string() around everything you receive from a web form!
User avatar
pavanpuligandla
Forum Contributor
Posts: 130
Joined: Thu Feb 07, 2008 8:25 am
Location: Hyderabad, India

Re: login php problem

Post by pavanpuligandla »

hii,
yes u've to use mysql_real_escape_string();
try this code and replace your line 31 with this.

Code: Select all

$query="SELECT * FROM login WHERE username='" . mysql_real_escape_string($username) . "' AND password='".   mysql_real_escape_string ($encrypt). "'";
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: login php problem

Post by mellowman »

thank you for helping but im getting anther error now :[

Code: Select all

Parse error: parse error, unexpected T_VARIABLE in web4designzsite/login/login.php on line 40

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
 
<body>
 
 
 
<?php
 
//Database Information
 
$dbhost = "------";
$dbname = "------";
$dbuser = "------";
$dbpass = "------";
 
 
//Connect to database
 
mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname) or die(mysql_error());
 
session_start();
$username = $_POST[‘username’];
$password = md5($_POST[‘password’]);
 
$query="SELECT * FROM login WHERE username='" . mysql_real_escape_string($username) . "' AND password='".   mysql_real_escape_string ($encrypt). "'";
 
$result = mysql_query($query);
 
if (mysql_num_rows($result) != 1) {
$error = “Bad_Login”;
    include “login.html”;
 
} else {
    <-------this is line 40---->$_SESSION[‘username’] = “$username”;
    include “memberspage.php”;
}
 
?>
 
 
</body>
</html>
User avatar
pavanpuligandla
Forum Contributor
Posts: 130
Joined: Thu Feb 07, 2008 8:25 am
Location: Hyderabad, India

Re: login php problem

Post by pavanpuligandla »

always use session_start(); at the top.

$_SESSION[‘username’] = “$username”; replace this with
$_SESSION['username']=$username;
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: login php problem

Post by mellowman »

..that worked :] but man im just full of errors today lol
sorry if this is <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> you off pavanpuligandla

Code: Select all

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php:12) in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 27
 
Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php:12) in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 27
 
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 35
 
Warning: main(“loginhtml”): failed to open stream: No such file or directory in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 37
 
Warning: main(“loginhtml”): failed to open stream: No such file or directory in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 37
 
Warning: main(): Failed opening '“loginhtml”' for inclusion (include_path='.:/usr/local/lib/php') in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 37
these are the errers the code was posted before
mellowman
Forum Commoner
Posts: 62
Joined: Sat Nov 22, 2008 5:37 pm

Re: login php problem

Post by mellowman »

anyone?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: login php problem

Post by Stryks »

Well ... the answer is all in your error messages (although I have to admit they don't just say, "here, fix it like this".
mellowman wrote:Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php:12) in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 27
We go to line 27 and see session_start(). Headers already sent ey? Well ... 5 minutes of research later (a search here or on php.net should turn up something) you'll find that no output can be sent to the browser before session_start(). The answer, move it up to line 1, without so much a space before it.
mellowman wrote:Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 35
We scroll on down to line 35 and take a look ... we find that $result was what we used in mysql_num_rows(). Ok, so the result was with $result as it's the only argument passed. We look up to the previous line of code and see how it was set. How could it not be a valid resource? Maybe the query has a problem ... a quick jump over the the PHP manual page for mysql_query() should give some insight on how to catch query errors. HERE (in particular, look at the use of die() in the first example).

While looking at this error, you'll want to pass your eye over the query ... just to make sure you cant see anything wrong that would cause the query to fail. My first thought is ... "What is $encrypt, and why is it being used to match the password"? A few lines above reveal where a point of confusion may have occurred.
mellowman wrote:Warning: main(“loginhtml”): failed to open stream: No such file or directory in /home/content/p/l/i/plinskidesign/html/web4designzsite/login/login.php on line 37
Hmmm .... the include statement on line 37 looks as though it should work ... but I'm wondering what the deal is with the unusual quotes. They look like the ones that get automatically switched out in MS Word and the like. I'd try using a plain text editor and changing them back to normal quotes. That's where I'd start anyhow.

Hope that helps.
Post Reply