2old2learn? Posted May 23, 2011 Share Posted May 23, 2011 Hey I stop trying at this for awhile ..clearing my head.but now am trying again...This is the error I am getting : Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\html_login\checklogin.php on line 14 <?php//$host="localhost"; // Host name //$username=""; // Mysql username //$password=""; // Mysql password //$db_name="test"; // Database name //$tbl_name="members"; // Table name // Connect to server and select databse.//mysql_connect("$host", "$username", "$password")or die("cannot connect"); //mysql_select_db("$db_name")or die("cannot select DB");include('config.php');// username and password sent from form $username('$_POST['username']'); <<<< Line 14$password('$_POST['password']'); // To protect MySQL injection (more detail about MySQL injection)$username = stripslashes('username');$password = stripslashes('password');$username = mysql_real_escape_string('username');$password = mysql_real_escape_string('password');$sql="(SELECT * FROM members WHERE username='username' and password='password')";$result=mysql_query($sql);// Mysql_num_row is counting table row$count=mysql_num_rows($result);// If result matched $myusername and $mypassword, table row must be 1 rowif($count==1){// Register $myusername, $mypassword and redirect to file "login_success.php"$session_register("username");$session_register("password"); header("location:login_success.php");}else {echo "Wrong Username or Password";}?> I gotta get this right one day....LOL Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 Okay made some changes and its getting better;changed this : $sql= ("SELECT * FROM 'members' WHERE username = 'username' and password = 'password'");$result=mysql_query($sql);// Mysql_num_row is counting table row$count=mysql_num_rows($result); <<< Error is here now get this error only; Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\html_login\checklogin.php on line 27Wrong Username or Passwordthanks Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 $username('$_POST['username']');$password('$_POST['password']');it will be like...$username($_POST['username']);$password($_POST['password']); Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 it will be like...$username($_POST['username']);$password($_POST['password']);ahhh cool thanks..Edit:It now gives this error:Fatal error: Call to undefined function root() in C:\xampp\htdocs\html_login\checklogin.php on line 14Here is the original script to line 14/15// username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; hope this helps alittle more... Link to comment Share on other sites More sharing options...
birbal Posted May 23, 2011 Share Posted May 23, 2011 that means There is some function named root() which is not defined. you have to define a function first to use it. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 There is some function named root() which is not defined. you have to define a function first to use it.Okay will look for it but don't recall seeing anything referring to it...will keep you posted...Edited:Created a function file<?phpfunction ($getname('$username') & ('$password'));?> I now get this error.. Parse error: syntax error, unexpected '(', expecting ')' in C:\xampp\htdocs\html_login\getuserpassword.php on line 2I hope I am getting close... Link to comment Share on other sites More sharing options...
thescientist Posted May 23, 2011 Share Posted May 23, 2011 what is that code trying to do exactly? did you look for root in your original script? Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 what is that code trying to do exactly? did you look for root in your original script?there is no original root ..in the script that I can see....Form<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form name="form1" method="post" action="checklogin.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td colspan="3"><strong>Member Login </strong></td></tr><tr><td width="78">Username</td><td width="6">:</td><td width="294"><input name="username" type="text" name="username"></td></tr><tr><td>Password</td><td>:</td><td><input name="password" type="text" password="password"></td></tr><tr><td>Â </td><td>Â </td><td><input type="submit" name="Submit" value="Login"></td></tr></table></td></form></tr></table> checklogin.php <<< Below is the original from the site I was trying to get login to work.. <?php$host="localhost"; // Host name $username=""; // Mysql username $password=""; // Mysql password $db_name="test"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse.mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");// username and password sent from form $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // To protect MySQL injection (more detail about MySQL injection)$myusername = stripslashes($myusername);$mypassword = stripslashes($mypassword);$myusername = mysql_real_escape_string($myusername);$mypassword = mysql_real_escape_string($mypassword);$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";$result=mysql_query($sql);// Mysql_num_row is counting table row$count=mysql_num_rows($result);// If result matched $myusername and $mypassword, table row must be 1 rowif($count==1){// Register $myusername, $mypassword and redirect to file "login_success.php"session_register("myusername");session_register("mypassword"); header("location:login_success.php");}else {echo "Wrong Username or Password";}?> that is all the files of the example I was trying to get to work...other than login_success.php Link to comment Share on other sites More sharing options...
thescientist Posted May 23, 2011 Share Posted May 23, 2011 well you got undefined for root, so somewhere you had this in your code root() and I was specifically referring to this, function ($getname('$username') & ('$password')); when I was asking what are you trying to do Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 well you got undefined for root, so somewhere you had this in your coderoot() and I was specifically referring to this, function ($getname('$username') & ('$password')); when I was asking what are you trying to do Oh sorry thought of making a function to call " username/password "as for the root() I didn't see it anywhere but will look it over again.. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 okay making some head way..found out the original code was for a later version of php..used a newer script of php..now just get this error... // Check if session is not registered , redirect back to main page. // Put this code in first line of web page. Deprecated: Function session_is_registered() is deprecated in C:\xampp\htdocs\html_login\login_success.php on line 5Login Successfulnewer code for checklogin.php now..<?phpob_start();$host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="tecicc"; // Database name $tbl_name="members"; // Table name // Connect to server and select databse.mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB");// Define $myusername and $mypassword $username=$_POST['username']; $password=$_POST['password']; // To protect MySQL injection (more detail about MySQL injection)$username = stripslashes($username);$password = stripslashes($password);$username = mysql_real_escape_string($username);$password = mysql_real_escape_string($password);$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";$result=mysql_query($sql);// Mysql_num_row is counting table row$count=mysql_num_rows($result);// If result matched $myusername and $mypassword, table row must be 1 rowif($count==1){// Register $myusername, $mypassword and redirect to file "login_success.php"session_register("username");session_register("password"); header("location:login_success.php");}else {echo "Wrong Username or Password";}ob_end_flush();?> main-login.php code <? session_start();if(!session_is_registered('username'))?><table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"><tr><form name="form1" method="post" action="checklogin.php"><td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td colspan="3"><strong>Member Login </strong></td></tr><tr><td width="78">Username</td><td width="6">:</td><td width="294"><input name="username" type="text" name="username"></td></tr><tr><td>Password</td><td>:</td><td><input name="password" type="text" password="password"></td></tr><tr><td>Â </td><td>Â </td><td><input type="submit" name="Submit" value="Login"></td></tr></table></td></form></tr></table> login_success.php // Check if session is not registered , redirect back to main page. // Put this code in first line of web page. <? session_start();if(!session_is_registered('username')){header('location:main_login.php');}?><html><body>Login Successful</body></html> hope this helps..at least I think I am getting closer... Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 LOL I think I finally got it too work..I now get " Login Successful " nothing else..Here is what I did...this was changed in " Main_login.php and login_success.php " files... <? // Check if session is not registered , redirect back to main page. // Put this code in first line of web page.session_start();if(isset($_Session_is_registered['username'])){ << made changes here by adding the " isset " header('location:main_login.php');}?><html><body>Login Successful</body></html> Cool..now too make another test of " header('location: home.php'); to see what happens...So I would say this is semi " Solved "..more things to looked into... Link to comment Share on other sites More sharing options...
2old2learn? Posted May 23, 2011 Author Share Posted May 23, 2011 okay finally figured out how to add the login box at the top of web page..view image below...Now I would like to lock out certain pages until user is logged in...so if you click a link it redirects you back to home..I need a lock out script..to go with the login script.. Link to comment Share on other sites More sharing options...
thescientist Posted May 24, 2011 Share Posted May 24, 2011 you need to use the $_SESSION global array to track a user across all your pages. When a user successfully logs in, you would want to set a member in the $_SESSION array to a testable value. i.e. <?php//if user submits username and password and it checks outsession_start();$_SESSION['logged_in'] = true;?> and then on any pages that you want to lock users from seeing without have successfully logged in, do something like this <?phpsession_start();if($_SESSION['logged_in']){ //show the page to the user}else{ header('Location: login.php');};?> http://us3.php.net/manual/en/book.session.php Link to comment Share on other sites More sharing options...
2old2learn? Posted May 24, 2011 Author Share Posted May 24, 2011 Hey;I found this little piece of script to lock out users until signed in... global $user;if ($user->uid) { <<<< This is Line 3 where the error occurs.. return "This page is only visible for logged-in users."; } else { echo "<script type='text/javascript'>alert('To Access this Page .. Please Log In!');</script>"; include('1home.php'); //echo "Please Log In!"; return; } ?> But I get this error message to go along with it.. Notice: Trying to get property of non-object in C:\xampp\htdocs\html_login\display.php on line 3Thanks Link to comment Share on other sites More sharing options...
thescientist Posted May 24, 2011 Share Posted May 24, 2011 is this snipper taken out of context? Do you have a user object in your script? Or a user class? Unless you have the accompanying code, you can just use $_SESSION because it's native. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 24, 2011 Author Share Posted May 24, 2011 Okay I think I am making some headway made the following changes.. <?php //This block prevents users from viewing this page unless logged insession_start();global $_user;if ($_user->username); { return "This page is only visible for logged-in users."; } else { <<< Line 6 error echo "<script type='text/javascript'>alert('To Access this Page .. Please Log In!');</script>"; include('1home.php'); //echo "Please Log In!"; return; } ?> now I get this error... Parse error: syntax error, unexpected T_ELSE in C:\xampp\htdocs\html_login\display.php on line 6 Link to comment Share on other sites More sharing options...
thescientist Posted May 24, 2011 Share Posted May 24, 2011 look where at if your statement. there's an errant semi-colon. also, what's the point of that if/else statement? It seem like either case ever shows the page. It also looks like it's a function you're taking out of context with the returns in both cases. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 24, 2011 Author Share Posted May 24, 2011 look where at if your statement. there's an errant semi-colon. also, what's the point of that if/else statement? It seem like either case ever shows the page. It also looks like it's a function you're taking out of context with the returns in both cases.Okay I get this error now..Notice: Trying to get property of non-object in C:\xampp\htdocs\html_login\display.php on line 4This is line 4:if ($_username->username) { Link to comment Share on other sites More sharing options...
2old2learn? Posted May 24, 2011 Author Share Posted May 24, 2011 LOL I this I am starting to get this..so far looks good got this to work..at least so far..Below is the change I just made...and now no error's if(isset($_username-> username)) { seems to be getting somewhere with this... Link to comment Share on other sites More sharing options...
thescientist Posted May 24, 2011 Share Posted May 24, 2011 do you need to check your variable names?edit: that doesn't really seem like the intent of that code. $_username->username implies that it should be returning something. have you checked if it does? it might be just giving you a false positive, depending on the code within your if/else blocks. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 24, 2011 Author Share Posted May 24, 2011 do you need to check your variable names?edit: that doesn't really seem like the intent of that code. $_username->username implies that it should be returning something. have you checked if it does? it might be just giving you a false positive, depending on the code within your if/else blocks.Now to think about it...your right it could be a false positive ..I think I will have to modify it..I think I know how..I will post my results later...thanks bud.. :)I tried this..not too sure if this makes any difference .. but I don't think so..if(isset($_members-> username)) Link to comment Share on other sites More sharing options...
thescientist Posted May 24, 2011 Share Posted May 24, 2011 does this say anything? <?php$username = $_members-> username;echo 'USERNAME => ' . $username;?> where did you find this script? do you have something we can follow along with?I still feel like $_SESSION would be the simplest and easiest option for you at this point in your learning. It's just an if/else statement in each page that needs to lock or not lock users out of a page and one line of code to start the session. Assuming a user has successfully logged in and $_SESSION['logged_in'] = true; <?phpsession_start();if($_SESSION['logged_in']){ //show the page to the user}else{ //lock them out header('Location: logout.php');};?> that's it, plus the code for your pages, which you already have. Link to comment Share on other sites More sharing options...
2old2learn? Posted May 24, 2011 Author Share Posted May 24, 2011 does this say anything?<?php$username = $_members-> username;echo 'USERNAME => ' . $username;?> where did you find this script? do you have something we can follow along with?I still feel like $_SESSION would be the simplest and easiest option for you at this point in your learning. It's just an if/else statement in each page that needs to lock or not lock users out of a page and one line of code to start the session. Assuming a user has successfully logged in and $_SESSION['logged_in'] = true; <?phpsession_start();if($_SESSION['logged_in']){ //show the page to the user}else{ //lock them out header('Location: logout.php');};?> that's it, plus the code for your pages, which you already have. I came across this script doing a google search..but can't seem to find it now...yea, your right should stick to the session route..until I get more familar with php...thanks... Link to comment Share on other sites More sharing options...
2old2learn? Posted May 24, 2011 Author Share Posted May 24, 2011 okay modified your script and works well with redirecting back to home page where login script is... <?php //This block prevents users from viewing this page unless logged insession_start();if(isset($_SESSION['logged_in'])){ //show the page to the user}else{ //lock them out header('Location: home.php');};?> Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.