我在php中遇到会话错误

时间:2017-08-18 12:49:56

标签: php

我正在尝试创建一个PHP登录系统,但在编写时遇到了这个错误:

  

session_start():无法发送会话缓存限制器 - 标头   发送

我创建了三个页面,index.php包含表单,logyourself.php包含检查PHP代码,home.php包含用户在成功登录时重定向到的页面

logyourself.php:

session_start();

$host     = ""; // Host name
$username = ""; // Mysql username
$password = ""; // Mysql password
$db_name  = ""; // Database name
$tbl_name = ""; // 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");
@mysql_query("set names 'utf8';");

// Define $myusername and $mypassword
$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 $tbl_name WHERE username='$username' and password='$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 "admin.php"

    $_SESSION['username'] = "username";
    $_SESSION['password'] = "password";
    header("location:home.php");
} else {
    echo "wrong Password or username";
    echo "<br>";
    echo "<a href=index.php>";
    echo "Back To Login";
    echo "</a>";
}

home.php:

session_start();
if (!isset($_SESSION['username'])) {
    header("location:index.php");
}
$username = $_SESSION['username'];
echo "Hi $username
        <br>
        <a href=\"logout.php\">Logout</a>
    ";

为什么我收到此错误?

0 个答案:

没有答案