Jump to content

PHP Login help


jubba890

Recommended Posts

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 :)

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458696
Share on other sites

  On 11/17/2013 at 9:54 PM, JIXO said:

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");
}
?>
Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458698
Share on other sites

  On 11/17/2013 at 9:54 PM, objnoob said:

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

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458699
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458704
Share on other sites

 
  On 11/17/2013 at 9:59 PM, jubba890 said:

 

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");
}
?>

 

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.
}
Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458745
Share on other sites

  On 11/18/2013 at 7:43 AM, Flail said:

 

 

 

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.

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458752
Share on other sites

  On 11/18/2013 at 1:39 AM, JIXO said:

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

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458765
Share on other sites

  On 11/18/2013 at 10:40 AM, jubba890 said:

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 ???

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458804
Share on other sites

  On 11/18/2013 at 2:03 PM, JIXO said:

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>
Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458907
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458910
Share on other sites

  On 11/18/2013 at 10:35 PM, JIXO said:

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

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458917
Share on other sites

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'];
Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458920
Share on other sites

  On 11/19/2013 at 12:19 AM, JIXO said:

 

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

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458927
Share on other sites

  On 11/19/2013 at 12:19 AM, JIXO said:

 

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!

Link to comment
https://forums.phpfreaks.com/topic/284001-php-login-help/#findComment-1458928
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.