jubba890 Posted November 17, 2013 Share Posted November 17, 2013 I have this code in login_seccess.php <?php session_start(); if(!session_is_registered(myusername)){ header("location:main_login.php"); } ?> <html> <body> Login Successful </body> </html> I also have this code in checklogin.php <?php $host="host"; // Host name $username="user"; // Mysql username $password="pass"; // Mysql password $db_name="main13"; // Database name $tbl_name="Login"; // Table name ?> Can I do like include("checklogin.php") and use the $host variables and such to connect to the database in login_success.php? I also need on login_seccess.php to display more information like their first, last, and e-mail I need help really fast! thank you Quote Link to comment Share on other sites More sharing options...
JIXO Posted November 17, 2013 Share Posted November 17, 2013 (edited) You can include checklogin.php using require_once(). require_once("/path/to/checklogin.php"); I did not catch what you're doing here : session_is_registered(myusername) is myusername has been defined or not ? To display the first and last name you must store them first in $_SESSION, the code will be similar to this echo $_SESSION['firstName']; Also session_is_registered() has been removed of PHP 5.4.0. Edited November 17, 2013 by JIXO Quote Link to comment Share on other sites More sharing options...
objnoob Posted November 17, 2013 Share Posted November 17, 2013 Yes, but your checklogin.php is incomplete and doesn't really do anything. login_success.php is lacking too. Here's some tutorials to get you on the right track. http://www.webhostingjams.com/web-development-top-5-php-and-mysql-login-scripttutorials/ Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 17, 2013 Author Share Posted November 17, 2013 You can include checklogin.php using require_once(). require_once("/path/to/checklogin.php"); I did not catch what you're doing here : session_is_registered(myusername) is myusername has been defined or not ? To display the first and last name you must store them first in $_SESSION, the code will be similar to this echo $_SESSION['firstName']; Also session_is_registered() has been removed of PHP 5.4.0. Here is the whole code for checklogin.php: <?php $host="host"; // Host name $username="user"; // Mysql username $password="pass"; // Mysql password $db_name="main13"; // Database name $tbl_name="Login"; // 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 row if($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"; sleep(2); header("location:main_login.php"); } ?> Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 17, 2013 Author Share Posted November 17, 2013 Yes, but your checklogin.php is incomplete and doesn't really do anything. login_success.php is lacking too. Here's some tutorials to get you on the right track. http://www.webhostingjams.com/web-development-top-5-php-and-mysql-login-scripttutorials/ I didn't post the whole code for checklogin.php Quote Link to comment Share on other sites More sharing options...
JIXO Posted November 18, 2013 Share Posted November 18, 2013 (edited) Hello Jubba890, session_register() also has been removed since PHP 5.4.0, in checklogin.php instead of writing : session_register("myusername"); session_register("mypassword"); Try this : $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['mypassword'] = $_POST['mypassword']; And in login_seccess.php try <?php session_start(); // Comment this part // if(!session_is_registered(myusername)){ // header("location:main_login.php"); // } // Try this if(!isset($_SESSION['myusername']) || !isset($_SESSION['mypassword'])) { header("location:main_login.php"); } ?> <html> <body> Login Successful <br /> Your username is : <?php echo $_SESSION["myusername"]."<br />"; ?> </body> </html> Sorry for the late reply. Edited November 18, 2013 by JIXO Quote Link to comment Share on other sites More sharing options...
Flail Posted November 18, 2013 Share Posted November 18, 2013 Here is the whole code for checklogin.php:<?php$host="host"; // Host name$username="user"; // Mysql username$password="pass"; // Mysql password$db_name="main13"; // Database name$tbl_name="Login"; // 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";sleep(2);header("location:main_login.php");}?> Your condition is wrong. read line by line again. I would not recommend you to put table name into the same file as the db connection.if ($count == 1) {// this means there is record found, therefore your register should be unsuccessful.} Quote Link to comment Share on other sites More sharing options...
MDCode Posted November 18, 2013 Share Posted November 18, 2013 Your condition is wrong. read line by line again. I would not recommend you to put table name into the same file as the db connection.if ($count == 1) { // this means there is record found, therefore your register should be unsuccessful. } The file is named checklogin.php. It seems to be a login. Not a registration. Quote Link to comment Share on other sites More sharing options...
KaiSheng Posted November 18, 2013 Share Posted November 18, 2013 I think he meant login. it's the same logic. Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 18, 2013 Author Share Posted November 18, 2013 Hello Jubba890, session_register() also has been removed since PHP 5.4.0, in checklogin.php instead of writing : session_register("myusername"); session_register("mypassword"); Try this : $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['mypassword'] = $_POST['mypassword']; And in login_seccess.php try <?php session_start(); // Comment this part // if(!session_is_registered(myusername)){ // header("location:main_login.php"); // } // Try this if(!isset($_SESSION['myusername']) || !isset($_SESSION['mypassword'])) { header("location:main_login.php"); } ?> <html> <body> Login Successful <br /> Your username is : <?php echo $_SESSION["myusername"]."<br />"; ?> </body> </html> Sorry for the late reply. I treid your code and when I replaced it,everytime I logged in it would clear the fiedls and thats all Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 18, 2013 Author Share Posted November 18, 2013 The file is named checklogin.php. It seems to be a login. Not a registration. I think he meant login. it's the same logic. Yes it is a login Quote Link to comment Share on other sites More sharing options...
JIXO Posted November 18, 2013 Share Posted November 18, 2013 (edited) I treid your code and when I replaced it,everytime I logged in it would clear the fiedls and thats all Well, clearing a table fiedls has something to do with an update code maybe in other page, review your code again, explain more. did login_successful.php print the users name ? After displaying the Login Successful message, what page is requested, can you paste the code ??? Edited November 18, 2013 by JIXO Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 18, 2013 Author Share Posted November 18, 2013 Well, clearing a table fiedls has something to do with an update code maybe in other page, review your code again, explain more. did login_successful.php print the users name ? After displaying the Login Successful message, what page is requested, can you paste the code ??? login_success.php did not even get displayed Here is the code: checklogin.php <?php $host="host"; // Host name $username="user"; // Mysql username $password="pass"; // Mysql password $db_name="main13"; // Database name $tbl_name="Login"; // 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 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['mypassword'] = $_POST['mypassword']; header("location:login_success.php"); } else { echo "Wrong Username or Password"; sleep(2); header("location:main_login.php"); } ?> login_success.php <?php session_start(); // Comment this part // if(!session_is_registered(myusername)){ // header("location:main_login.php"); // } // Try this if(!isset($_SESSION['myusername']) || !isset($_SESSION['mypassword'])) { header("location:main_login.php"); } ?> <html> <body> Login Successful <br /> Your username is : <?php echo $_SESSION["myusername"]."<br />"; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
JIXO Posted November 18, 2013 Share Posted November 18, 2013 Hi jubba890, I made a mistake in checklogin.php, add session start at line two <?php session_start(); $host="host"; // Host name ...... ?> And in checklogin.php change $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['mypassword'] = $_POST['mypassword']; To $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; I copied your code, made these changes and worked !, if you have any other problem please post it. Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 18, 2013 Author Share Posted November 18, 2013 Hi jubba890, I made a mistake in checklogin.php, add session start at line two <?php session_start(); $host="host"; // Host name ...... ?> And in checklogin.php change $_SESSION['myusername'] = $_POST['myusername']; $_SESSION['mypassword'] = $_POST['mypassword']; To $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; I copied your code, made these changes and worked !, if you have any other problem please post it. It worked perfectly! How would I get the value for other things like their first name and e-mail. I think I got how to display it on login_success.php Quote Link to comment Share on other sites More sharing options...
Solution JIXO Posted November 19, 2013 Solution Share Posted November 19, 2013 I'm happy to hear that, to get the other fields in th DB we use mysql_fetch_assoc() Add these lines in checklogin.php // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Fetch the result to get the users information $userInfo = mysql_fetch_assoc($result); // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['email'] = $userInfo['email']; // and so on ... $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("location:login_success.php"); } Remember that the index email in $userInfo is a the field name from the table Login, so now you may replace $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; With $_SESSION['myusername'] = $userInfo['username']; $_SESSION['mypassword'] = $userInfo['password']; Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 19, 2013 Author Share Posted November 19, 2013 (edited) I'm happy to hear that, to get the other fields in th DB we use mysql_fetch_assoc() Add these lines in checklogin.php // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Fetch the result to get the users information $userInfo = mysql_fetch_assoc($result); // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['email'] = $userInfo['email']; // and so on ... $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("location:login_success.php"); } Remember that the index email in $userInfo is a the field name from the table Login, so now you may replace $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; With $_SESSION['myusername'] = $userInfo['username']; $_SESSION['mypassword'] = $userInfo['password']; I did what you suggested and it did not work here is the code for checklogin.php <?php session_start(); $host="host"; // Host name $username="user"; // Mysql username $password="pass"; // Mysql password $db_name="main13"; // Database name $tbl_name="Login"; // 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 row // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Fetch the result to get the users information $userInfo = mysql_fetch_assoc($result); // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['email'] = $userInfo['e-mail']; // and so on ... $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("location:login_success.php"); } else { echo "Wrong Username or Password"; sleep(2); header("location:main_login.php"); } ?> and for login_success.php <?php session_start(); if(!isset($_SESSION['myusername']) || !isset($_SESSION['mypassword'])) { header("location:main_login.php"); } ?> <html> <body> Login Successful <br /> Your username is : <?php echo $_SESSION["myusername"]."<br />"; ?> Your email is : <?php echo $_SESSION["email"]."<br />"; ?> </body> </html> It does not display the username or email. It dosent even get to that part. I also tried replacing $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; With $_SESSION['myusername'] = $userInfo['username']; $_SESSION['mypassword'] = $userInfo['password']; This just cleared the fields. I am pretty sure it didn't get the data from the table Edited November 19, 2013 by jubba890 Quote Link to comment Share on other sites More sharing options...
jubba890 Posted November 19, 2013 Author Share Posted November 19, 2013 I'm happy to hear that, to get the other fields in th DB we use mysql_fetch_assoc() Add these lines in checklogin.php // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Fetch the result to get the users information $userInfo = mysql_fetch_assoc($result); // Register $myusername, $mypassword and redirect to file "login_success.php" $_SESSION['email'] = $userInfo['email']; // and so on ... $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; header("location:login_success.php"); } Remember that the index email in $userInfo is a the field name from the table Login, so now you may replace $_SESSION['myusername'] = $myusername; $_SESSION['mypassword'] = $mypassword; With $_SESSION['myusername'] = $userInfo['username']; $_SESSION['mypassword'] = $userInfo['password']; I figured it out you need to have " instead of ' for $userInfo['password'] Thank you so much! Quote Link to comment 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.