/    Sign up×
Community /Pin to ProfileBookmark

login page script issues

Hi guys, creating a log in script so my users can login to my website.

This is the code so far

[code=php]<!DOCTYPE html>
<html>
<head>
<title>Richard Hemmings </title>
<meta name=”viewpoint” content=”width=device-width, intail-scale=1.0″>
<link href = “css/bootstrap.min.css”rel = “stylesheet”>
<link href = “css/stylesheet.css” rel = “stlesheet”>

</head>
<body>

<div class = “navbar navbar-inverse navbar-static-top”>
<div class = “container”>
<div class = “navbar navbar-default navbar-fixed-bottom”>

<div class = “container”>
<P class = “navbar-text”>Site build and belongs to Richard Hemmings (C) 2015</P>

</div>

</div>
<script src = “http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js”></script><script src = “js/bootstrap/js” ></script>

</body>

</html>

<?php
include(‘inc/header.php’);
require(‘connection_db.php’);

if (isset($_POST[‘submit’])){
$uname = mysql_escape_string($_POST[‘uname’]);
$pass = mysql_escape_string ($_POST[‘pass’]);
$pass = md5 ($pass) ;

$uname = mysql_query(“SELECT * FROM `users` WHERE `uname` = ‘$uname’ AND `pass` = ‘$pass'”);
if(mysql_num_rows($sql) > 0 {
echo “you are now logged in successfully!”;
exit ();

}else{
echo “Wrong Username and password entered please try again”;
}

}else{

echo $form = <<<EOT
<form action”login.php” method=”POST”>
Username: <input type=”text” name =”uname” /><br />
password: <input type=”password” name=”pass” /><br />
<input type=”submit” name=”submit” value=”Log in” />
</form>

EOT;
}

?>

[/code]

This is the error message that I’m getting at the moment

Parse error: syntax error, unexpected ‘{‘ in C:xampphtdocslogin.php on line 37

Any help would be brilliant sorry again for been a apain

to post a comment
PHP

24 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 12.2015 — Not sure if this is line 37, but it's what jumped out at me:
[code=php]
if(mysql_num_rows($sql) > 0 {
[/code]

You left out the closing ")" for the if() condition. (You need another after the one that closes the mysql_num_rows() call)

BTW, the usual public service message: you really should stop using the mysql_*() functions and update things to either use mysql[b]i[/b] or the PDO extension, as the original MySQL extension is now deprecate in PHP (and therefore may no longer be supported in future releases).
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 13.2015 — Not sure if this is line 37, but it's what jumped out at me:
[code=php]
if(mysql_num_rows($sql) > 0 {
[/code]

You left out the closing ")" for the if() condition. (You need another after the one that closes the mysql_num_rows() call)

BTW, the usual public service message: you really should stop using the mysql_*() functions and update things to either use mysql[b]i[/b] or the PDO extension, as the original MySQL extension is now deprecate in PHP (and therefore may no longer be supported in future releases).[/QUOTE]



I've done what you suggested and this is the code now

[code=php]<?php
include('inc/header.php');
require('connection_db.php');
if (isset($_POST['submit'])){
$uname = mysql_escape_string($_POST['uname']);
$pass = mysql_escape_string ($_POST['pass']);
$pass = md5 ($pass) ;
$uname = mysql_query("SELECT * FROM users WHERE uname = '$uname' AND pass = '$pass'");
if(mysql_num_rows($sql)) > 0 {
echo "you are now logged in successfully!";
exit ();
}else{
echo "Wrong Username and password entered please try again";
}
}else{
echo $form = <<<EOT
<form action"login.php" method="POST">
Username: <input type="text" name ="uname" /><br />
password: <input type="password" name="pass" /><br />
<input type="submit" name="submit" value="Log in" />
</form>
EOT;
}
?>[/code]


I'm now getting the following error message

Parse error: syntax error, unexpected '>' in C:xampphtdocslogin.php on line 39
Copy linkTweet thisAlerts:
@advent_geekMar 13.2015 — I've done what you suggested and this is the code now

I'm now getting the following error message

Parse error: syntax error, unexpected '>' in C:xampphtdocslogin.php on line 39
[/QUOTE]


[code=php]
if(mysql_num_rows($sql)) > 0 {

use the following instead of above

if(mysql_num_rows($sql) > 0 ) {

[/code]
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 13.2015 — [code=php]
if(mysql_num_rows($sql)) > 0 {

use the following instead of above

if(mysql_num_rows($sql) > 0 ) {

[/code]
[/QUOTE]


thanks for that I'm getting the same eorror as i did when I created my register page, this is the error now

Parse error: syntax error, unexpected 'action' (T_STRING), expecting ',' or ';' in C:xampphtdocslogin.php on line 47
Copy linkTweet thisAlerts:
@rootMar 13.2015 — try changing [code=php] EOT;
} [/code]
to [code=php]EOT;
} [/code]

so that EOT; is sitting with no spaces before it and on a line of its own.
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 14.2015 — try changing [code=php] EOT;
} [/code]
to [code=php]EOT;
} [/code]

so that EOT; is sitting with no spaces before it and on a line of its own.[/QUOTE]


I've changed the code and this is how it looks now

[code=php]<!DOCTYPE html>
<html>
<head>
<title>Richard Hemmings </title>
<meta name="viewpoint" content="width=device-width, intail-scale=1.0">
<link href = "css/bootstrap.min.css"rel = "stylesheet">
<link href = "css/stylesheet.css" rel = "stlesheet">

</head>
<body>

<div class = "navbar navbar-inverse navbar-static-top">
<div class = "container">
<div class = "navbar navbar-default navbar-fixed-bottom">

<div class = "container">
<P class = "navbar-text">Site build and belongs to Richard Hemmings (C) 2015</P>

</div>

</div>
<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><script src = "js/bootstrap/js" ></script>




</body>

</html>

<?php
include('inc/header.php');
require('connection_db.php');
if (isset($_POST['submit'])){
$uname = mysql_escape_string($_POST['uname']);
$pass = mysql_escape_string ($_POST['pass']);
$pass = md5 ($pass) ;
$uname = mysql_query("SELECT * FROM users WHERE uname = '$uname' AND pass = '$pass'");
if(mysql_num_rows($sql) > 0 ) {

echo "you are now logged in successfully!";
exit ();
}else{
echo "Wrong Username and password entered please try again";
}
}else{
echo $form =<<<EOT
//<form action"login.php" method="POST">
Username: <input type="text" name ="uname" /><br />
password: <input type="password" name="pass" /><br />
<input type="submit" name="submit" value="Log in" />
</form>
EOT;
}

?>[/code]


this is the error message that I'm now getting

Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in C:xampphtdocslogin.php on line 54
Copy linkTweet thisAlerts:
@ginerjmMar 14.2015 — Why do you keep posting code with no line numbers in it, or without a comment from YOU telling us the indicated line number?
Copy linkTweet thisAlerts:
@NogDogMar 14.2015 — [code=php]
echo $form =<<<EOT
[/code]

That line is confusing: do you want to echo out $form, or do you want to assign the following text to $form? Assuming you just want to echo, it's simpler to just get out of PHP mode at that point:
[code=php]
}else{
?>
<form action"login.php" method="POST">
Username: <input type="text" name ="uname" /><br />
password: <input type="password" name="pass" /><br />
<input type="submit" name="submit" value="Log in" />
</form>
<?php } ?>
[/code]
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 15.2015 — [code=php]
echo $form =<<<EOT
[/code]

That line is confusing: do you want to echo out $form, or do you want to assign the following text to $form? Assuming you just want to echo, it's simpler to just get out of PHP mode at that point:
[code=php]
}else{
?>
<form action"login.php" method="POST">
Username: <input type="text" name ="uname" /><br />
password: <input type="password" name="pass" /><br />
<input type="submit" name="submit" value="Log in" />
</form>
<?php } ?>
[/code]
[/QUOTE]


I want to echo out the form but also want the form to work where the user fills in the info and they can log into the website
Copy linkTweet thisAlerts:
@rootMar 15.2015 — Now... change [code=php]echo $form =<<<EOT [/code]

to

[code=php]echo <<<EOT [/code]

or

[code=php]$form =<<<EOT [/code]

if you are echoing the string later then the variable would be used.
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 15.2015 — Now... change [code=php]echo $form =<<<EOT [/code]

to

[code=php]echo <<<EOT [/code]

or

[code=php]$form =<<<EOT [/code]

if you are echoing the string later then the variable would be used.[/QUOTE]


I'm using the following

[code=php] $form =<<<EOT[/code]

I'm still getting the following


Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in C:xampphtdocslogin.php on line 54

This is my code now
[code=php]<!DOCTYPE html>
<html>
<head>
<title>Richard Hemmings </title>
<meta name="viewpoint" content="width=device-width, intail-scale=1.0">
<link href = "css/bootstrap.min.css"rel = "stylesheet">
<link href = "css/stylesheet.css" rel = "stlesheet">

</head>
<body>

<div class = "navbar navbar-inverse navbar-static-top">
<div class = "container">
<div class = "navbar navbar-default navbar-fixed-bottom">

<div class = "container">
<P class = "navbar-text">Site build and belongs to Richard Hemmings (C) 2015</P>

</div>

</div>
<script src = "http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script><script src = "js/bootstrap/js" ></script>




</body>

</html>

<?php
include('inc/header.php');
require('connection_db.php');
if (isset($_POST['submit'])){
$uname = mysql_escape_string($_POST['uname']);
$pass = mysql_escape_string ($_POST['pass']);
$pass = md5 ($pass) ;
$uname = mysql_query("SELECT * FROM users WHERE uname = '$uname' AND pass = '$pass'");
if(mysql_num_rows($sql) > 0 ) {

echo "you are now logged in successfully!";
exit ();
}else{
echo "Wrong Username and password entered please try again";
}
}else{
$form =<<<EOT
<form action"login.php" method="POST">
Username: <input type="text" name ="uname" /><br />
password: <input type="password" name="pass" /><br />
<input type="submit" name="submit" value="Log in" />
</form>
echo EOT;
}

?>[/code]
Copy linkTweet thisAlerts:
@NogDogMar 15.2015 — The end of the heredoc "<<<" assignment, which in this case you've chosen to be "EOT;", must be the only thing on that line and with no white-space of any kind before it.

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
Copy linkTweet thisAlerts:
@rootMar 15.2015 — I have also noted some odd behaviour or HEREDOC methods, namely an errant whitespace character on the EOT; line that follows the ; has on occasion had me on a bug hunt.

I would also suggest that you remove any white space characters and also add a further line space between :

EOT;
}

to get
EOT;

}
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 15.2015 — I have also noted some odd behaviour or HEREDOC methods, namely an errant whitespace character on the EOT; line that follows the ; has on occasion had me on a bug hunt.

I would also suggest that you remove any white space characters and also add a further line space between :

EOT;
}

to get
EOT;

}
[/QUOTE]


Hey I have done what you suggested and still getting the following ?

Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in C:xampphtdocslogin.php on line 55

I feel like banging my head against a brick wall, I have tried loads of different things now, again thanks so much for the help and support your giving me and to everyone else! Its much appreciated
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 15.2015 — Hey I have done what you suggested and still getting the following ?

Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in C:xampphtdocslogin.php on line 55

I feel like banging my head against a brick wall, I have tried loads of different things now, again thanks so much for the help and support your giving me and to everyone else! Its much appreciated[/QUOTE]


Please ignore what I have just said!! it works whoooohoooo!! I didnt copy and past your suggestion and now it works thank you soo much
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 15.2015 — sorry again for this!!

When I now try and log in with a test username and password I'm now getting the following error

Notice: Undefined variable: sql in C:xampphtdocslogin.php on line 39

Warning: mysql_num_rows() expects parameter 1 to be resource, null given in C:xampphtdocslogin.php on line 39

Wrong Username and password entered please try again
Copy linkTweet thisAlerts:
@rootMar 15.2015 — You need to swap all mysql_* functions for mysqli_* functions

when using a procedural approach to code, you will need to pass a reference that should be stored in a variable when you open a database connection. for example

[code=php]$con = mysqli_connect("localhost","username","password","database");
//...
$result = mysqli_query( $con, "SELECT * ... [/code]
Copy linkTweet thisAlerts:
@rootMar 15.2015 — to tidy up what you already have
[code=php]$uname = mysql_escape_string($_POST['uname']);
$pass = mysql_escape_string ($_POST['pass']);
$pass = md5 ($pass) ;
$uname = mysql_query("SELECT * FROM users WHERE uname = '$uname' AND pass = '$pass'");
if(mysql_num_rows($sql) > 0 ) { [/code]

to

[code=php]// assumed $con = mysqli_connect(...);
$user = filter_var( trim( $_POST['uname'] ), FILTER_SANITIZE_STRING );
$pass = md5( filter_var( trim( $_POST['pass'] ), FILTER_SANITIZE_STRING) );
$qry = sprintf( "SELECT * FROM users WHERE uname='%s' AND pass='%s' LIMIT 1;",
mysqli_escape_string( $con, $user ),
mysqli_escape_string( $con, $pass )
);
// run the query
$result = mysqli_query( $con, $qry );

if( mysqli_affected_rows( $result ) > 0 ) {
...
[/code]
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 16.2015 — to tidy up what you already have
[code=php]$uname = mysql_escape_string($_POST['uname']);
$pass = mysql_escape_string ($_POST['pass']);
$pass = md5 ($pass) ;
$uname = mysql_query("SELECT * FROM users WHERE uname = '$uname' AND pass = '$pass'");
if(mysql_num_rows($sql) > 0 ) { [/code]

to

[code=php]// assumed $con = mysqli_connect(...);
$user = filter_var( trim( $_POST['uname'] ), FILTER_SANITIZE_STRING );
$pass = md5( filter_var( trim( $_POST['pass'] ), FILTER_SANITIZE_STRING) );
$qry = sprintf( "SELECT * FROM users WHERE uname='%s' AND pass='%s' LIMIT 1;",
mysqli_escape_string( $con, $user ),
mysqli_escape_string( $con, $pass )
);
// run the query
$result = mysqli_query( $con, $qry );

if( mysqli_affected_rows( $result ) > 0 ) {
...
[/code]
[/QUOTE]


I will give it ago. I have defined the connection to the database in another file that I have called
[code=php]require('connection_db.php'); [/code] I will let you know what comes of it
Copy linkTweet thisAlerts:
@rootMar 16.2015 — I assume that you create a database connection when you include the file.

if your database does not use $con = mysqli_connect( ... then you will encounter problems as myqsl_ and mysqli_ are not interchangeable, so it will pay to swap now as upgrades to web hosts and PHP versions upgrades will see all mysql_ support dropped and you could be left in the dark again and need to convert. So do it now!
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 16.2015 — I assume that you create a database connection when you include the file.

if your database does not use $con = mysqli_connect( ... then you will encounter problems as myqsl_ and mysqli_ are not interchangeable, so it will pay to swap now as upgrades to web hosts and PHP versions upgrades will see all mysql_ support dropped and you could be left in the dark again and need to convert. So do it now![/QUOTE]


right ok so how on earth do i do that then? as my register script works perfect, I have added quite a few "test" users so far and it works perfect so does the connection file
Copy linkTweet thisAlerts:
@rootMar 16.2015 — Where do you make your database connection?

You must be using a connection handler of some sorts, if this is only on your development server, you will encounter problems in a real world environment.
Copy linkTweet thisAlerts:
@stokie-ruchauthorMar 17.2015 — Where do you make your database connection?

You must be using a connection handler of some sorts, if this is only on your development server, you will encounter problems in a real world environment.[/QUOTE]


This is where I make the connection in my code

[code=php]require('connection_db.php');[/code]

This is the connection code (please note that it works when I register a user)

[code=php]<?php
mysql_connect ("localhost", "root", "");
mysql_select_db ("tut");



?> [/code]
Copy linkTweet thisAlerts:
@rootMar 17.2015 — You need to be using mysqli_* functions throughout, mysql_* functions have been depreciated and in many deployment environments will not work as mysql has been removed, everyone has to move up to mysqli_* functions, so it is better if you replace all mysql_* functions.

in the example here.[code=php] <?php
mysql_connect ("localhost", "root", "");
mysql_select_db ("tut");



?> [/code]
you would [code=php] <?php
$con = mysqli_connect("localhost", "root", "", "tut");

?> [/code]
or if you prefer an OO method [code=php] <?php
$con = new mysqli("localhost", "root", "","tut");
?> [/code]
×

Success!

Help @stokie-ruch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...