Jump to content

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\wam


Reece112233

Recommended Posts

Having a problem with a php code.

Im new to php and have only been learning for one day but have had previous experiances with php in which i have copied and pasted and edited to suit my needs .

 

This is my code

<?php

   $username = $_POST["user"];
   $password = $_POST["pass"];
   $login = $_GET["login"];
   
   setcookie("username","$username",time()+86400);
   
   if($login=="yes") {
      
      $con = mysql_connect("localhost","root","arceye");
      
      mysql_select_db("login");
      
      $get = mysql_query("SELECT count(id) FROM login WHERE user='$username' and pass='$password'");
      
      $result = mysql_result($get, 0);
      
      if($result!=1) {
         echo "Invalid Username Or Password";
      } else {
         echo "Login Successful"
         $_SESSION["username"] = $username;
      }
   }

 

i named that login.php

 

im running a wamp server thinge on localhost.

 

when i try to run it on http://localhost/login.php i get the error

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\wamp\www\login.php on line 23

 

can anyone help

i had 5 other error but managed to use common sense to fix them this one i cant seem to work out. :(

 

Thanks in advance

 

MOD EDIT: code tags added.

try this:

 

<?php
session_start();
$username = trim($_POST["user"]);
$password = trim($_POST["pass"]);
$login = trim($_GET["login"]);
setcookie("username","$username",time()+86400);
if($login == "yes"){
$con = mysql_connect("localhost","root","arceye") or die('Cannot connect to server...');
mysql_select_db("login",$con) or die('Cannot select that database...');
$get = mysql_query("SELECT count(`id`) as total FROM `login` WHERE `user`='$username' and `pass`='$password'",$con) or die(mysql_error());      
$res = mysql_fetch_assoc($get);
@mysql_close($con);
$result = $res['total'];
if($result !=1 ){
	echo "Invalid Username Or Password";
	exit();
}else{
	echo "Login Successful"
	$_SESSION["username"] = $username;
}
}
?>

Nope it dosent work although i like this code better it now says

Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\login.php on line 3

and what is a typo

this is the code i wrote i dont see any differance in the 3rd line

<?php
session_start
$username = trim($_POST["user"]);
$password = trim($_POST["pass"]);
%login = trim($_GET["login"]);
setcookie("username","$username",time()+86400)
if($login == "yes"){
   $con = mysql_connect("localhost","root","arceye") or die('Cannot connect to server...');
   mysql_select_db("login",$con) or die ('Cannot select that database...');
   $get = mysql_query("SELECT count(`id`) as total FROM `login` WHERE `user`='$username' and `pass`='$password'",$con) or die(mysql_error());      
   $res = mysql_fetch_assoc($get);
   @mysql_close($con);
   $result = $res['total'];
   if($result !=1 ){
      echo "Invalid Username Or Password";
      exit();
   }else{
      echo "Login Successful"
      $_SESSION["username"] = $username;
   }
}
?>

thanks...

 

MOD EDIT: code tags added.

another problem. got one on  line 5 but fixed by changing % to $ a typo.. (i know what a typo is now) and line 7 the line b4 it was wrong fixed that now im onto line 19 error is

Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\wamp\www\login.php on line 19

 

this is my code (well my code improved by you) so far

<?php
session_start();
$username = trim($_POST["user"]);
$password = trim($_POST["pass"]);
$login = trim($_GET["login"]);
setcookie("username","$username",time()+86400);
if($login == "yes"){
   $con = mysql_connect("localhost","root","arceye") or die('Cannot connect to server...');
   mysql_select_db("login",$con) or die ('Cannot select that database...');
   $get = mysql_query("SELECT count(`id`) as total FROM `login` WHERE `user`='$username' and `pass`='$password'",$con) or die(mysql_error());      
   $res = mysql_fetch_assoc($get);
   @mysql_close($con);
   $result = $res['total'];
   if($result !=1 ){
      echo "Invalid Username Or Password";
      exit();
   }else{
      echo "Login Successful"
      $_SESSION["username"] = $username;
   }
}
?>

thanks once again

because your script expects two POSTed variables and one from the url ($_GET):

 

$username = trim($_POST["user"]);
$password = trim($_POST["pass"]);
$login = trim($_GET["login"]);

 

to test, just use:

 

$username = 'Admin';
$password = 'oliver';
$login = 'yes';

oh i see you know your very clever. i added a @ before the $username stuff on the  6 , 7 , 8 line of the code and that sorted out the database not found error. i also added another user and pass to my database . the user is john and the pass is 1234 the id is 2

 

im now getting a blank webpage after i login nothing happens .. the website url is http://localhost/login.php?username=Admin&password=oliver

i thought there might be an error and its not showing up so i edited the code to do error reporting but thats not working i just get a blank screen..

 

i have decided to include my html code in this bit because the part thats not working is a visual part usually handled by html .

 

the html code is

<html>
<body>

   <form action="login.php" method"post">
      Username: <input type="text" name="username" />
      <br>
      Password <input type="password" name="password" />
      <br>
      <input type="submit" value="Login" />
   </form>
</body>
<html>

 

and the new php code is

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
session_start();
@$username = trim($_POST["user"]);
@$password = trim($_POST["pass"]);
@$login = trim($_GET["login"]);
setcookie("username","$username",time()+86400);
if($login == "yes"){
   $con = mysql_connect("localhost","root","arceye") or die('Cannot connect to server...');
   mysql_select_db("login",$con) or die ('Cannot select that database...');
   $get = mysql_query("SELECT count(`id`) as total FROM `login` WHERE `user`='$username' and `pass`='$password'",$con) or die(mysql_error());      
   $res = mysql_fetch_assoc($get);
   @mysql_close($con);
   $result = $res['total'];
   if($result !=1 ){
      echo "Invalid Username Or Password";
      exit();
   }else{
      echo "Login Successful";
      $_SESSION["username"] = $username;
   }
}
?>

 

 

 

NOTE: these are two separate files login.html and login.php both being stored in the www directory of my wamp server and can be accesses by typing

 

localhost/login.html

or

localhost/login.php

 

thanks

 

MOD EDIT: code tags added.

As I understand it, you are not being sent back to login.html after processing form on login.php.  Try

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
session_start();
@$username = trim($_POST["user"]);
@$password = trim($_POST["pass"]);
@$login = trim($_GET["login"]);
setcookie("username","$username",time()+86400);
if($login == "yes"){
   $con = mysql_connect("localhost","root","arceye") or die('Cannot connect to server...');
   mysql_select_db("login",$con) or die ('Cannot select that database...');
   $get = mysql_query("SELECT count(`id`) as total FROM `login` WHERE `user`='$username' and `pass`='$password'",$con) or die(mysql_error());     
   $res = mysql_fetch_assoc($get);
   @mysql_close($con);
   $result = $res['total'];
   if($result !=1 ){
      echo "Invalid Username Or Password";
      exit();
   }else{
      echo "Login Successful";
      $_SESSION["username"] = $username;
header("location: login.html");
   }
}
?>

@ is bad coding practice in my opinion.  Do it right, and save yourself some trouble in the long run.

 

@$username = trim($_POST["user"]);
@$password = trim($_POST["pass"]);
@$login = trim($_GET["login"]);

Should be:

$username = (!emtpy($_POST['user'])) ? trim($_POST["user"]) : NULL;
$password = (!empty($_POST['pass'])) ? trim($_POST["pass"] : NULL;
$login = (isset($_GET['login'])) ? trim($_GET["login"]) : NULL;

ok ive edited my html code to this

<html>
<body>

   <form action="login.php if($login == "yes"){" method"post">
      Username: <input type="text" name="username" />
      <br>
      Password <input type="password" name="password" />
      <br>
      <input type="submit" value="Login" />
   </form>
</body>
<html>

 

 

and my php file to this

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
session_start();
$username = (!emtpy($_POST['user'])) ? trim($_POST["user"]) : NULL;
$password = (!empty($_POST['pass'])) ? trim($_POST["pass"] : NULL;
$login = (isset($_GET['login'])) ? trim($_GET["login"]) : NULL;
setcookie("username","$username",time()+86400);
if($login == "yes"){
   $con = mysql_connect("localhost","root","arceye") or die('Cannot connect to server...');
   mysql_select_db("login",$con) or die ('Cannot select that database...');
   $get = mysql_query("SELECT count(`id`) as total FROM `login` WHERE `user`='$username' and `pass`='$password'",$con) or die(mysql_error());      
   $res = mysql_fetch_assoc($get);
   @mysql_close($con);
   $result = $res['total'];
   if($result !=1 ){
      echo "Invalid Username Or Password";
      exit();
   }else{
      echo "Login Successful";
      $_SESSION["username"] = $username;
   header("location: login.html");
   }
}
?>

ok?

 

MOD EDIT: code tags added.

im now getting an error saying

 

Parse error: syntax error, unexpected ':' in C:\wamp\www\login.php on line 7

 

i tried changing the : to a ; which just said

 

Parse error: syntax error, unexpected ';' in C:\wamp\www\login.php on line 7

 

i tried removing it completely and the screen went blank..

 

so i just clicked undo and left it as it is with the @ although if anyone would to come up with a solution to that problem i would use the person whos name begins with j (soz cant see name from the post reply screen) idea.. thanks

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.