php - 从我的 MySQL 表检查登录信息

标签 php mysql session

我这里有代码检查我的 MySQL 表的登录信息,然后我希望继续我的 session ,但它似乎不起作用。

这段代码正在检查我的登录,它是 check_login.php

 <?php

    $host="localhost"; // Host name 
    $username="root"; // Mysql username 
    $password=""; // Mysql password 
    $db_name="student"; // Database name 
    $tbl_name="login"; // Table name 

    // Connect to server and select database.
    mysql_connect("$host", "$username", "$password", "$db_name")or die("cannot connect1");
    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'";
    //$sqlaccesslevel="SELECT AccessLevel FROM $tbl_name WHERE Username='$myusername' and Password='$mypassword'";
    $result = mysql_query($sql) or die(mysql_error());
    //$resultaccesslevel = mysql_query($sqlaccesslevel) or die(mysql_error());

    // Mysql_num_row is counting table row
    if ($result)
      $count=mysql_num_rows($result);
    else
      $count = 0;

    // If result matched $myusername and $mypassword, table row must be 1 row
    if($count==1){
    // Register $myusername, $mypassword and redirect to the website"
    session_start();
    $_SESSION['username'] = "$username";


    header("location:student.html");
    }
    else {
    echo "Wrong Username or Password. YOU LOOSE. GOOD DAY SIR.";
    }


    ?>

这是我的login.html页面,当用户输入详细信息时会发布到我的check_login.php

  <html>
    <head> 
    </head>
    <body>
    <table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
    <form name="form1" method="post" action="check_login.php">
    <td>
    <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
    <tr>
    <td colspan="3"><strong>Student Login</strong></td>
    </tr>
    <tr>
    <td width="78">Username</td>
    <td width="6">:</td>
    <td width="294"><input name="myusername" type="text" id="myusername"></td>
    </tr>
    <tr>
    <td>Password</td>
    <td>:</td>
    <td><input name="mypassword" type="text" id="mypassword"></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td><input type="submit" name="Submit" value="Login"></td>
    </tr>
    </table>
    </td>
    </form>
    </tr>
    </table>
    </body>
    </html>

我将其输出到student.html,登录似乎可以正常工作,但 session 没有提供任何帮助,非常感谢。

<html>
<head>
</head>
<body>


<h1>hello
</h1>

<?php  // this starts the session 
 session_start();
if( isset($_SESSION["sessionusername"]) )
{
header("location:student.html");
}
 // echo variable from the session, we set this on our other page 
 echo "Welcome":  
  echo $_SESSION['user'];
?>





</body>
</html>

最佳答案

session_start(); 放在您的 php 文件顶部,同时 read this topicthis这是关于在 php 和配置指令中使用 session 。

关于php - 从我的 MySQL 表检查登录信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20492140/

相关文章:

php - htaccess 尾部斜线问题

php - AS2 多字符串替换

php - 转义 $_SESSION 用户名以便在 mysql 查询中使用的正确方法是什么?

c# - 在整个 session 中维护数据表和变量

PHP - 如何重命名对象属性?

php - .htaccess 为本地主机端口映射重写?

mysql - 使用 MySQL 创建循环排列的优雅方法

session - 在 ASP.NET Core MVC Identity 中部署 Docker 后自动注销

java - 防止 Hibernate session 刷新/存储无效的脏实体

php - 在Mac上使用vscode和mamp的php可执行文件的路径