Reece112233 Posted August 31, 2011 Share Posted August 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/ Share on other sites More sharing options...
Reece112233 Posted August 31, 2011 Author Share Posted August 31, 2011 oh yeh sorry i have a html script and im quite confident it couldnt be that because im quite experiance in html Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264086 Share on other sites More sharing options...
WebStyles Posted August 31, 2011 Share Posted August 31, 2011 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264091 Share on other sites More sharing options...
Reece112233 Posted August 31, 2011 Author Share Posted August 31, 2011 thanks im writing it into notepad++ now i cant copy and paste cause it all comes out in one line Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264097 Share on other sites More sharing options...
WebStyles Posted August 31, 2011 Share Posted August 31, 2011 typing it is not a good idea... it will be very easy for you to get a typo. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264098 Share on other sites More sharing options...
Reece112233 Posted August 31, 2011 Author Share Posted August 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264100 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 im using php 5 btw it comes with wamp Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264101 Share on other sites More sharing options...
WebStyles Posted September 1, 2011 Share Posted September 1, 2011 see? first typo: session_start should be session_start(); Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264102 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264104 Share on other sites More sharing options...
WebStyles Posted September 1, 2011 Share Posted September 1, 2011 missing semicolon after: echo "Login Successful" should be: echo "Login Successful"; Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264106 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 ok thats done the script is fine its now saying undefined index on line 3 , 4 and 5 . dunno why though the databases are definately there [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264108 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 so i clicked login database , browse login table .. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264109 Share on other sites More sharing options...
WebStyles Posted September 1, 2011 Share Posted September 1, 2011 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'; Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264113 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264114 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 the first part where it says oh i see you know your clever i meant to put oh i see . you know, your really clever Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264115 Share on other sites More sharing options...
Drummin Posted September 1, 2011 Share Posted September 1, 2011 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"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264117 Share on other sites More sharing options...
WebStyles Posted September 1, 2011 Share Posted September 1, 2011 you didn't add login=yes, your script only runs based on this condition: if($login == "yes"){ Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264121 Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 @ 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; Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264122 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264123 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264126 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 i still get a blank screen Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264127 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 and the login ==yes thing is already in the php script 10th line Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264130 Share on other sites More sharing options...
jcbones Posted September 1, 2011 Share Posted September 1, 2011 $password = (!empty($_POST['pass'])) ? trim($_POST["pass"] : NULL; //<- trim() is missing the closing parenthesis. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264132 Share on other sites More sharing options...
Reece112233 Posted September 1, 2011 Author Share Posted September 1, 2011 yeah that done it jchones no more error but i still got a blank screen you got any ideas for that? Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264137 Share on other sites More sharing options...
Pikachu2000 Posted September 1, 2011 Share Posted September 1, 2011 I've edited your posts to add . . . BBCode tags. Please use them in the future. Quote Link to comment https://forums.phpfreaks.com/topic/246144-parse-error-syntax-error-unexpected-t_variable-expecting-or-in-cwam/#findComment-1264139 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.