Jump to content

Login script


Twist3d

Recommended Posts

I have a problem of missing the most obvious things.

But I'm trying to create a login script.

Here is the error:

 

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in *****/login.php on line 47

 

Here is the code around line 47

 

case 'login2':
session_start();
require_once 'connect.php';

    $username = $_POST['user'];
    $escaped_username = mysql_real_escape_string($username);
    $md5_password = md5($_POST['pass']);
    
    $query = mysql_query("select * from users where Username = $username and Password = $md5_password"')
    $num_rows = mysql_num_rows($result);


            
if($num_rows == 1){
$result = mysql_fetch_assoc($query); 
                   
$_SESSION['User'] = $_POST['user'];    
echo "Logged in";
}

else{
echo "Wrong Username or Password";
}
break;

 

That is the case of login?action=login2

Any help?

 

Link to comment
https://forums.phpfreaks.com/topic/205927-login-script/
Share on other sites

<?php
session_start();
$toswitch = $_GET['action'];
switch($toswitch){

default:
if (isset($_SESSION['Admin'])){
echo "You are already logged in! <br> Click <a href='addproject.php'>Here</a> to add a project";
} else{
?>

<!--HTML Form -->

<form name="login_form" method="post" action="login.php?action=login2">
  <input name="user" type="text" id="user">
  Username<br />
  <input name="pass" type="password" id="pass">
  Password<br />
  <input type="submit" name="login" id="login" value="Login" />
</form>

<?php
//Ending the case by breaking it
}
break;

case 'login2':
session_start();
require_once 'connect.php';

    $username = $_POST['user'];
    $escaped_username = mysql_real_escape_string($username);
    $md5_password = md5($_POST['pass']);
    
    $query = mysql_query("select * from users where Username = $username and Password = $md5_password"')
    $num_rows = mysql_num_rows($result);


            
if($num_rows == 1){
$result = mysql_fetch_assoc($query); 
                   
$_SESSION['User'] = $_POST['user'];    
echo "Logged in";
}

else{
echo "Wrong Username or Password";
}
break;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/205927-login-script/#findComment-1077569
Share on other sites

You have a Syntax error (look at the code you posted where it all starts to go red) your syntax error is above that.

 

$query = mysql_query("select * from users where Username = $username and Password = $md5_password"')

 

A few things wrong with this.

A. the ' at the very end, this is a syntax error.

B. There is no ; at the very end, another syntax error.

C. MySQL requires single quotes around text / string data.

 

Here is that line corrected:

$query = mysql_query("select username from users where Username = '$username' and Password = '$md5_password' LIMIT 1");

 

Just an anal part of me, you only return data you need. Since you do not use any data received from the query just pull one record and one column, this will improve efficiency of the query.

 

I have not looked anywhere else, but that should take care of your syntaxual error for that part at least.

Link to comment
https://forums.phpfreaks.com/topic/205927-login-script/#findComment-1077580
Share on other sites

Change this line to get the mysql error:

 

$query = mysql_query("select username from users where Username = '$username' and Password = '$md5_password' LIMIT 1") or trigger_error("Username Password Query Failed: " . mysql_error());

 

Should tell you where the problem lies within the query.

Link to comment
https://forums.phpfreaks.com/topic/205927-login-script/#findComment-1077607
Share on other sites

Archived

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

×
×
  • 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.