0

我在这里做了一个简单的登录脚本。有3个文件,1个是functions.php(包含登录功能),然后是userdashboard.php,其中包含一些用户功能,然后是另一个文件users.php,用于处理登录。

问题是,每当我登录时,登录成功但会引发错误:-未知变量用户名

它应该显示登录人的用户名,我做错了什么?这是代码:-

函数.php

<?php
include 'dbconnector.php';
function checklogin($username,$password)
{
    include 'dbconnector.php';
    $userexists=false;
    $username=mysql_real_escape_string($username);
    $password=mysql_real_escape_string($password);
    $password=md5($password);
    $query="select * from f_users where username = '" . $username . "' and password = '" . $password . "'";
    $result=mysql_query($query,$db) or die (mysql_error($db));
    if(mysql_num_rows($result) > 0)
    {
        $userexists=true;
    }
    else
    {
        $userexists=false;
    }
    return $userexists;
}

用户仪表板.php

<?php
include('dbconnector.php');
session_start();
if(isset($_SESSION['logged']) && $_SESSION['logged']=1)
{
    $_SESSION['username']=$username;
    echo "Hello " . $username;
}
else
{
    header('Location:login.php');
}
echo '<a href="logout.php">logout</a>';
?>

处理登录的文件。

include 'functions.php';
.
.
.
case 'login':
        $username=$_POST['username'];
        $password=$_POST['password'];
        $username=mysql_real_escape_string($username);
        $password=mysql_real_escape_string($password);
        $password=md5($password);
        if((!empty($username)) && (!empty($password)))
        {
            if(!checklogin($username,$password))
            {
                $_SESSION['logged']=1;
                $_SESSION['username']=$username;
                header('Location:userdashboard.php');
            }
            else
            {
                echo "Invalid combination of username and password";
                echo "redirecting to the login page";
                header('refresh:2;URL=login.php');
            }
        }
        else
        {
            echo "username or password fields cannot be empty, redirecting";
            header('refresh:2;URL=login.php');
        }
        break;

感谢您修复 Houssni。我这里有一个奇怪的错误。

即使我尝试了用户名和密码的有效组合,它总是会转到 else 部分并抛出错误。我在这里做错了什么?

$username=mysql_real_escape_string($username);
            $password=mysql_real_escape_string($password);
            $password=md5($password);
            $query="select * from f_users where username = '" . $username . "' and password = '" . $password . "'";
            $result=mysql_query($query,$db) or die (mysql_error($db));
            if(mysql_num_rows($result) > 0)
            {
                session_start();
                $_SESSION['logged']=1;
                $_SESSION['username']=$username;
                header('Location:userdashboard.php');
                exit();
            }
            else
            {
                echo mysql_num_rows($result);
                echo "Invalid combination of username and password";
                echo "redirecting to the login page";
                header('refresh:2;URL=login.php');
                exit();
            }
4

3 回答 3

2

您的 userdashboard.php 中还有另一个错误

改变

if(isset($_SESSION['logged']) && $_SESSION['logged']=1)
{
$_SESSION['username']=$username;
echo "Hello " . $username;
}

if(isset($_SESSION['logged']) && $_SESSION['logged']==1)
{
  $_SESSION['username']=$username;
   echo "Hello " . $username;
}
于 2013-11-06T12:05:37.267 回答
2

$username是一个参数,因此您只能在其函数范围内使用它。通过该函数获取用户名$_POST$_SESSION在该函数中设置。或者userdashboard.php你应该再次分配变量$username并给出它的值。

并且在functions.php此函数中还有另一个包含。如果您调用此函数,您将包含此文件两次。

在你打电话给你的最后,header("Location: ")你应该打电话exit(),因为否则它将继续运行该页面的 PHP 代码。

于 2013-11-06T12:03:24.630 回答
0

session_start() 函数需要包含在条件部分中,

    if((!empty($username)) && (!empty($password)))
    {
        if(!checklogin($username,$password))
        {
            session_start(); // added session start
            $_SESSION['logged']=1;
            $_SESSION['username']=$username;
            header('Location:userdashboard.php');
            exit();
        }
        else
        {
            echo "Invalid combination of username and password";
            echo "redirecting to the login page";
            header('refresh:2;URL=login.php');
            exit();
        }
    }
于 2013-11-06T12:14:56.867 回答