Jump to content

PHP Login help


jubba890
Go to solution Solved by JIXO,

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

Edited by JIXO
Link to comment
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.

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

Edited by JIXO
Link to comment
Share on other sites

 

 

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
Share on other sites

 

 

 

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

I treid your code and when I replaced it,everytime I logged in it would clear the fiedls and thats all

Link to comment
Share on other sites

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 by JIXO
Link to comment
Share on other sites

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

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
Share on other sites

  • Solution

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
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'];

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 by jubba890
Link to comment
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'];

I figured it out you need to have " instead of ' for $userInfo['password']

Thank you so much!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.