Jump to content

[SOLVED] Login Session is not creating


bob2588

Recommended Posts

hello i wrote this php login but it not login me in can you guys take a look at it for me

<?php
include 'db.php';
// username and password sent from form 
$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 admin WHERE login='$username' and pass1='$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 row

if($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";
}
?>

Link to comment
https://forums.phpfreaks.com/topic/178752-solved-login-session-is-not-creating/
Share on other sites

Hi bob2588,

 

The tutorial you may have taken this code from here shows the login_success.php file to contain:

 

<? 
session_start();
if(!session_is_registered(myusername)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

 

Because in your code you are registering "username" (session_register("username");) and not "myusername" as the above code shows, change your code to read:

 

<? 
session_start();
if(!session_is_registered(username)){
header("location:main_login.php");
}
?>

<html>
<body>
Login Successful
</body>
</html>

 

Hope this helps.

WARNING: This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

 

session_register() should be avoided since (as stated above), it's soon to be removed, and is already deprecated.

 

instead of:

 

session_register("username");
session_register("password");

 

use:

 

$_SESSION['username'] = 'username here';
$_SESSION['password'] = 'password here';

 

read more about $_SESSION's

 

EDIT: and then access the $_SESSION like so:

 

if (!isset ($_SESSION['username'])) {
     header("location: main_login.php");
}

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.