1

再次您好,所以我发现的 php 登录脚本出错了,所以我不明白问题出在哪里,所以我在这里发布
这个应该如何工作:: 这应该像人输入他的用户名和密码一样工作,它得到使用存储在数据库服务器中的用户名或密码进行验证

PHP 代码

<?php       
ob_start();
$host = "localhost"; // Host name 
$username = "root"; // Mysql username 
$password = ""; // Mysql password 
$db_name = "test"; // Database name 
$tbl_name = "admin"; // 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");

// Define $myusername and $mypassword 
$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";
}
ob_end_flush();
?>

致命错误:在第 33 行的 /Applications/XAMPP/xamppfiles/htdocs/LOL/admin/checklogin.php 中调用未定义函数 session_register()

HTML 代码

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="assets/ico/favicon.png">

    <title>TFTRG</title>

    <!-- Bootstrap core CSS -->
    <link href="dist/css/bootstrap.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="jumbotron.css" rel="stylesheet">

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
    <script src="assets/js/html5shiv.js"></script>
    <script src="assets/js/respond.min.js"></script>
    <![endif]-->
</head>
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
    <tr>
        <form name="form1" method="post" action="checklogin.php">
            <td>
                <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
                    <tr>
                        <td colspan="3"><strong>Member 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>

<script src="assets/js/jquery.js"></script>
<script src="dist/js/bootstrap.min.js"></script>
<body>
<html>

login_success.php

<?php
session_start();
if(isset($_SESSION['myusername']))
{ 
   echo '';

}
else
{ 
header("location:login.php"); 
}
?>

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

谢谢

4

3 回答 3

1

您需要开始会话

session_start();
于 2013-08-23T04:09:35.573 回答
0

session_start()在 PHP 代码的开头添加

<?php
session_start();

访问变量如

$_SESSION['myusername']而不是session_register('myusername');因为后者已被弃用。

这是您修改后的代码。

<?php
session_start();
ob_start();
$host = "localhost"; // Host name 
$username = "root"; // Mysql username 
$password = ""; // Mysql password 
$db_name = "test"; // Database name 
$tbl_name = "admin"; // 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");

// Define $myusername and $mypassword 
$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']=$myusername;
    $_SESSION['mypassword']=$mypassword;
    header("location:login_success.php");
} else {
    echo "Wrong Username or Password";
}
ob_end_flush();
?>
于 2013-08-23T06:49:14.013 回答
0

您可以像这样使用会话,并且在会话中存储密码是一个坏习惯

利用

$_SESSION['myusername']=$myusername;

代替

session_register("$myusername");

你应该像这样使用会话

if(isset($_SESSION['myusername']))
{ 
   header("location:index.php"); 
}
else
{ 
header("location:login.php"); 
}
于 2013-08-23T04:12:53.233 回答