简单登录系统问题


Simple Login System Issues

所以我正在学习如何为我正在制作的网站制作一个简单的登录系统,我得到这个错误。

注意:使用未定义的常量myusername -假设'myusername'在/home/dkitterm/public_html/index.php第4行

这是第3-6行。

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

这也是我的支票登录页面。

        <?php
    $host="localhost";
    $username="dkitterm";
    $password="";
    $db_name="";
    $tbl_name="members";
    //connect to server and db
    mysql_connect("$host", "$username", "$password") or die("Server Down");
    mysql_select_db("$db_name") or die("cannot select DB");
    //username and password sent from form
    $myusername=$_POST['myusername'];
    $mypassword=$_POST['mypassword'];
    //debunk
    $myusername = stripslashes($myusername);
    $mypassword = stripslashes($mypassword);
    $myusername = mysql_real_escape_string($myusername);
    $mypassword = mysql_real_escape_string($mypassword);
    //encrypt password
    $encrypted_mypassword=md5($mypassword);

    $sql="SELECT * FROM members WHERE login='$myusername' and password='$encrypted_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: index.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>

@Col。Sharpanel说这是另一个更好的解决方案,因为你使用了一个贬值的函数。

 session_start();
    if(!isset($_SESSION['myusername'])){
     header("location: main_login.php");
    }
  <?php
$host="localhost";
$username="dkitterm";
$password="";
$db_name="";
$tbl_name="members";
//connect to server and db
mysql_connect("$host", "$username", "$password") or die("Server Down");
mysql_select_db("$db_name") or die("cannot select DB");
//username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
//debunk
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
//encrypt password
$encrypted_mypassword=md5($mypassword);

$sql="SELECT * FROM members WHERE login='$myusername' and password='$encrypted_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'] = $myusername; 
//if you need to register the password do the same but for the password 
header("location: index.php");
}
else {
echo "Wrong Username or Password";
}
?>

首先,你的支票页不能100%保护你,所以你需要

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

并在myusername周围加上引号,否则它会将其视为常量而不是