Error creating custom Login script under PHP

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1487 Views - Last Post: 02 November 2008 - 05:30 AM Rate Topic: -----

#1 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Error creating custom Login script under PHP

Posted 31 October 2008 - 08:25 AM

I have this line in my file

$query = “select * from members where username=’$username’ and password=’$password’”;



that gives me this error:

Parse error: syntax error, unexpected T_STRING in /home/a2004459/public_html/checklogin.php on line 16

Can anyone help as this looks fine but theres a problem some where.

The whole Code:

<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name

//Connect to database

mysql_connect ( $host, $username, $password)or die("Could not connect: ".mysql_error());
mysql_select_db($db_name) or die(mysql_error());

session_start();
$username = $_POST[‘username’];
$password = md5($_POST[‘password’]);

$query = "select * from members where username='$username' and password='$password'";

$result = mysql_query($query);

if (mysql_num_rows($result) != 1) {
$error = “Bad Login”;
	include “login.php”;

} else {
	$_SESSION[‘username’] = “$username”;
	include “main.php”;
}
?>


This post has been edited by sam_benne: 31 October 2008 - 08:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Error creating custom Login script under PHP

#2 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 433
  • View blog
  • Posts: 789
  • Joined: 17-June 08

Re: Error creating custom Login script under PHP

Posted 31 October 2008 - 08:48 AM

What's with all the curly-quotes? Are you coding in a word processor or copying this from a web page or something? You should be using plain-old ASCII straight-quotes - the " and ' characters. Curly quotes aren't the same thing and they won't work.
Was This Post Helpful? 0
  • +
  • -

#3 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Error creating custom Login script under PHP

Posted 31 October 2008 - 08:58 AM

Well I have changed the " and ' now it does nothing. It just clears the textboxes.

So I changed the line:
$query = "select * from members where username='$username' and password='$password'";



To:
$sql = 'select * from members where username=\'$username\' and password=\'$password\''; 



To see if it made a difference. Which it did it has a new error.


Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/a2004459/public_html/checklogin.php on line 21

Any ideas?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2486
  • View blog
  • Posts: 8,526
  • Joined: 08-August 08

Re: Error creating custom Login script under PHP

Posted 31 October 2008 - 09:07 AM

mysql_num_rows($result) is likely to be > 1, so you all you do is include login.php.
Was This Post Helpful? 0
  • +
  • -

#5 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Error creating custom Login script under PHP

Posted 31 October 2008 - 09:10 AM

Do you know how i can get this to work as I have been working on this all day and i'm not the best at php.
Was This Post Helpful? 0
  • +
  • -

#6 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: Error creating custom Login script under PHP

Posted 31 October 2008 - 10:37 AM

<?php
$host=""; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name

//Connect to database

mysql_connect ( $host, $username, $password)or die("Could not connect: ".mysql_error());
mysql_select_db($db_name) or die(mysql_error());

session_start();
$username = $_POST[‘username’];
$password = $_POST[‘password’];
 
$query = "select * from members where username='".$username."' and password='".md5($password)."' "; 

$result = mysql_query($query);
$result1 = mysql_num_rows($result);

if ($result1 != 1) {
	header(location: “login.php”);

} else {
	echo "Sorry could not log in. Please check your details and try again."; 
}
?>


changed a whole bunh of code there for you, should work now. also i made it redirect to login.php instead of just an include.

for you query use double qoutes, ", also just doing \'$password\' will look for just that. so it will look for a user with the name $username and a pass of $password.
to solve this you must end the sql search and then concatenate the strings so '". will use the single quote first to show that it is a seperate piece of information then the double quotes ends the string and the . concatenates the string. then to reconcatenate you just use a fullstop then double quotes and then a single quot like normal

also have changed
$username = $_POST[‘username’];
$password = $_POST[‘password’];


and then added the md5() to the select password in the query. this is how mine is set up but and i dont know if it makes a differance or not but it might.

i think that is it

regards
Matty
Was This Post Helpful? 0
  • +
  • -

#7 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: Error creating custom Login script under PHP

Posted 31 October 2008 - 11:03 AM

also it looks like your using 000webhost.com so i would change your login to default.php :)
Was This Post Helpful? 0
  • +
  • -

#8 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Error creating custom Login script under PHP

Posted 01 November 2008 - 03:35 AM

Well I did what you said and it hasn't worked. So when I log in all it does is change the url from default.php to checklogin.php and yeah i'm using 000webhost. I'm just wandering if it is because I have more fields in the database.
Was This Post Helpful? 0
  • +
  • -

#9 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: Error creating custom Login script under PHP

Posted 01 November 2008 - 06:36 AM

checklogin.php/default.php
<?php
// checkLogin.php

session_start(); // Start a new session
require('conn.php'); // Holds all of our database connection information

function cleanUpString($str){
	//this will clean up your strings and make it easier for you to impliment in to your code
	//note: mysql_real_escape_string requires an active database connection - so remove it if there is none
	$str = mysql_real_escape_string($str);
	$str = stripslashes($str);
	return $str;
}


if(isset($_POST['submit'])){
	// Get the data passed from the form
	$username = cleanUpString($_POST['uname_txt']);
	$password = cleanUpString($_POST['pwd_txt']);
	// Add only the required column names instead of * and if you only want one result, let the server know with a LIMIT 1
	//note the md5() wrapping round password - this will match the posted password with its md5 encrypted version.
	//have a look at the last bit of code (the add user part) to see how to add encrypted passwords into your database
	//this will add security to your application
	$sql = "select username from members where username = '".$username."' and password = '".md5($password)."' LIMIT 1";
	$result = mysql_query($sql)or die(mysql_error());
	$count = mysql_num_rows($result); // counts numbers of rows returned ( 1 if successful, 0 if not )
	if ($count > 0) {
		 $_SESSION['loggedIn'] = "true";
		 $_SESSION['user'] = $username;
		 $_SESSION['pass'] = md5($password);
		 $id = mysql_query("select id from members where username = '".$_SESSION['user']."' and password='".$_SESSION['pass']."'") or die(mysql_error());  
		 $row = mysql_fetch_array($id);  
		 $_SESSION['id'] = $row['id'] ;
	 
		 header("Location: home.php.php"); // This is wherever you want to redirect theuser to
	}else{
		$msg = 'Login Failed - Username and Password match not found';
	}
}
?>

<html>
<head>
</head>
<body>
<?php echo $msg; ?>
<p align="right" valign="middle">
<form name="loginfrm" id="loginfrm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
Username:<br />
<input type="text" name="uname_txt" id="uname_txt" /><br />
Password:<br />
<input type="password" name="pwd_txt" id="pwd_txt" /><br />
<input type="submit" name="submit" id="submit" value="Log In!" />
</form>

not got a user yet ???
<a href="register.php" target="_top">sign up here</a>
</p>


</body>
</html>


conn.php
<?php
//conn.php
$servername='';
$dbusername='';
$dbpassword='';
$dbname='';

function connecttodb($servername,$dbname,$dbuser,$dbpassword){
	global $dblink;
	$dblink = mysql_connect ("$servername","$dbuser","$dbpassword")or die('CONNECTION ERROR: Could not connect to MySQL');
	mysql_select_db("$dbname",$dblink) or die ('Could not open database'.mysql_error());
}

connecttodb($servername,$dbname,$dbusername,$dbpassword);

?>



hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#10 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Error creating custom Login script under PHP

Posted 01 November 2008 - 09:47 AM

I have it in looks great still nothing something is wrong but i have no idea I have tried 6 or more different ones and nothing seems to work. The register does yay. But useless if the users can't login.
Was This Post Helpful? 0
  • +
  • -

#11 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: Error creating custom Login script under PHP

Posted 01 November 2008 - 10:59 AM

please show your current code - all of it
Was This Post Helpful? 0
  • +
  • -

#12 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Error creating custom Login script under PHP

Posted 01 November 2008 - 02:08 PM

Its the same as above. Nothing changed other than database information. I am just wandering if it is because there are more fields in the database as it holds more data.
<?php
// Login.php

session_start(); // Start a new session
require('conn.php'); // Holds all of our database connection information

function cleanUpString($str){
	//this will clean up your strings and make it easier for you to impliment in to your code
	//note: mysql_real_escape_string requires an active database connection - so remove it if there is none
	$str = mysql_real_escape_string($str);
	$str = stripslashes($str);
	return $str;
}


if(isset($_POST['submit'])){
	// Get the data passed from the form
	$username = cleanUpString($_POST['uname_txt']);
	$password = cleanUpString($_POST['pwd_txt']);
	// Add only the required column names instead of * and if you only want one result, let the server know with a LIMIT 1
	//note the md5() wrapping round password - this will match the posted password with its md5 encrypted version.
	//have a look at the last bit of code (the add user part) to see how to add encrypted passwords into your database
	//this will add security to your application
	$sql = "select username from members where username = '".$username."' and password = '".md5($password)."' LIMIT 1";
	$result = mysql_query($sql)or die(mysql_error());
	$count = mysql_num_rows($result); // counts numbers of rows returned ( 1 if successful, 0 if not )
	if ($count > 0) {
		 $_SESSION['loggedIn'] = "true";
		 $_SESSION['user'] = $username;
		 $_SESSION['pass'] = md5($password);
		 $id = mysql_query("select id from members where username = '".$_SESSION['user']."' and password='".$_SESSION['pass']."'") or die(mysql_error());  
		 $row = mysql_fetch_array($id);  
		 $_SESSION['id'] = $row['id'];
	
		 header("Location: main.php"); // This is wherever you want to redirect theuser to
	}else{
		$msg = 'Login Failed - Username and Password match not found';
	}
}
?>

<head>
<title>Visual Basic 6 Tutorials</title>
<meta http-equiv="Content-Language" content="English" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
</head>
<body>

<div id="wrap">

<div id="header">
<h1><a href="index.php">Sam's Visual Basic</a></h1>
<h2>My Area, Teaching Visual Basic</h2>

</div>

<div id="right">

<h2><a href="login.php">Login</a></h2>
<div class="articles">


<?php echo $msg; ?>
<form name="loginfrm" id="loginfrm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
			<table cellpadding="5" cellspacing="5" border="0">
			  <tr>
				<td>Username: </td><td><input type="text" name="uname_txt" id="uname_txt" /></td>
			  </tr>
			  <tr>
				<td>Password: </td><td><input type="password" name="pwd_txt" id="pwd_txt" /></td>
			  </tr>
			  <tr>
				<td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="Log In!" /><input type="reset" value="Reset" class="buttons"></td>
			  </tr>
			  <tr>
				<td colspan="2" align="center">
				  <b>
					<font size="2">
					  <br />
					  <a href="register.php">Register</a> |
					  <a href="forgotpass.php">Forgot you Pass?</a>
					</font>
				  </b>
				</td>
			  </tr>
			</table>

</div>
</div>

<?php include("includes/menu.php"); ?>
<br>

<div style="clear: both;"> </div>
<?php include("includes/footer.php"); ?>
<br>
<br>
<p>&copy; 2008 Sam Bennett and Bennett Corporations.</p>
</div>



</body>
</html>


This post has been edited by sam_benne: 01 November 2008 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#13 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: Error creating custom Login script under PHP

Posted 02 November 2008 - 02:00 AM

message me your conn.php
over pm

also
no it wont be i have about 10 columns in mine and it still works. does it echo the error message ??

does it give a mysql error message ?

put an

echo $count;

somewhere to see whats going on

i reckon it is your conn.php

also, humuor me, take out the id="submit" in your form and just leave name ="submit"
Was This Post Helpful? 1
  • +
  • -

#14 sam_benne  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 16
  • View blog
  • Posts: 732
  • Joined: 16-January 08

Re: Error creating custom Login script under PHP

Posted 02 November 2008 - 02:44 AM

I have changed the id="submit" nothing happens. I've added the echo$count; nothing shows up. It shows no error anywhere. All it seems to do is refresh the page.
Was This Post Helpful? 0
  • +
  • -

#15 ghqwerty  Icon User is offline

  • if($spareTime > 0){ $this->writeCode(); }
  • member icon

Reputation: 40
  • View blog
  • Posts: 876
  • Joined: 08-August 08

Re: Error creating custom Login script under PHP

Posted 02 November 2008 - 02:51 AM

<form method="post" action="login.php">


try that
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2