【问题标题】:can't get SOME session variables across pages无法跨页面获取某些会话变量
【发布时间】:2012-05-18 04:12:20
【问题描述】:

尝试为我的会话变量添加一些额外的元素以用于文件系统目录工作,但我注意到我无法添加一些。这是我所拥有的:

    <?php
#login.php

// This page processes the login form submission.

// Upon successful login, the user is redirected.

// Two included files are necessary.

// Check if the form has been submitted:

if(isset($_POST['submitted']))
{

    // For processing the login:

    require_once ('login_functions.php');

    // Need the database connection:

    require_once ('../mysqli_connect.php');

    // Check the login:

    list ($check, $data) = check_login($dbc, $_POST['email'], $_POST['pass']);

    if ($check) //OK!
    {
        // set the session data:

      session_start();

      $_SESSION['user_id'] = $data['user_id'];

      $_SESSION['first_name'] = $data['first_name'];

      $_SESSION['company_name'] = $data['company_name'];

      $_SESSION['email'] = $data['email'];


      // Store the HTTP_USER_AGENT:

      $_SESSION['agent'] = md5($_SERVER['HTTP_USER_AGENT']);

        //Redirect:

        $url = absolute_url ('loggedin.php');

        header("Location: $url");

        exit(); // Quit the script.

    }
    else // Unsuccessful!
    {

       // Assign $data to $errors for error reporting
        // in the login_functions.php file.

        $errors = $data;

    }

    mysqli_close($dbc); // Close the database connection


} //End of the main submit conditional

//Create the page:

include('login_page_inc.php');



?>

这里是登录功能:

    <?php #login_functions.php

//This page defines two functions used by the login/logout process.

/*This function determines and returns an absolute URL.
 * It takes one argument: the page that concludes the URL.
 * The argument defaults to index.php
 */

function absolute_url ($page = 'about.php')
{
    //Start defining the URL...
    //URL is http:// plus the host name plus the current directory:

    $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);

    // Remove any trailing slashes:

    $url = rtrim($url, '/\\');

    // Add the page:
    $url .= '/' . $page;

    // Return the URL:

    return $url;

}//End of absolute_url() function.

/*This function validates the form data (email address and password).
 * If both are present, the database is queried.
 * The function requires a database connection
 * The function returns an array of information, including:
 *  - a TRUE/FALSE variable indicating success
 * - an array of either errors or the database result
 */

function check_login($dbc, $email = '', $pass = '')
{
    $errors = array(); // Initialize error array.

    // Validate the email address:

    if (empty($email))
    {
        $errors[] = 'You forgot to enter your email address.';
    }
    else
    {
        $e = mysqli_real_escape_string($dbc, trim($email));
    }

    // Validate the password:

    if (empty($pass))
    {
        $errors[] = 'You forgot to enter your password.';
    }
    else
    {
        $p = mysqli_real_escape_string($dbc, trim($pass));
    }

    if(empty($errors)) //If everything's OK.
    {
        // Retrieve the user_id and first_name for that email/password combo

        $q = "SELECT user_id, first_name, email FROM
            user WHERE email='$e' AND pass=SHA1('$p')";

        $r = @mysqli_query ($dbc, $q); // Run the query.

        //Check the result:

        if (mysqli_num_rows($r)==1)
        {
            //Fetch the record:

            $row = mysqli_fetch_array($r, MYSQLI_ASSOC);

            // Return true and the record:

            return array (true, $row);


        }
        else //Not a match for writer, check the publisher table
        {
            $q = "SELECT pub_id, company_name, cemail FROM
                pub WHERE cemail='$e' AND password=SHA1('$p')";

            $r = @mysqli_query ($dbc, $q);

            if (mysqli_num_rows($r)==1)
         {
            //Fetch the record:

            $row = mysqli_fetch_array($r, MYSQLI_ASSOC);

            // Return true and the record:

            return array (true, $row);

        }
        else
        {
            echo '<p>Invalid Credentials</p>';

         }
        }

    } // End of empty($errors) IF.

    // Return false and the errors:

    return array(false, $errors);

} // End of check_login() function.


?>

注意:$_SESSION['first_name'] 和 $_SESSION['company_name'] 一直可以正常工作,但是添加电子邮件和 user_id 不起作用。提前致谢。

【问题讨论】:

  • 尝试记录你在 $data 中的内容,可能 $data['user_id'] 和 $data['email'] 中没有值
  • 经过仔细检查,user_id 正常工作,留下的电子邮件已损坏。
  • 添加这段代码,看看有没有值出现 echo($data['user_id']);回声($data['email']);
  • 我尝试在欢迎屏幕上与用户的名字一起回显此内容,但无法获得回显值。

标签: php mysql session-variables


【解决方案1】:

email 和 user_id 永远不会对发布者起作用:因为登录函数返回“pub_id”和“cemail”。要解决此问题,您可以将 SQL 更改为:

        $q = "SELECT pub_id as user_id, company_name, cemail AS email FROM 
            pub WHERE cemail='$e' AND password=SHA1('$p')"; 

【讨论】:

  • 好的,谢谢。但是,为什么我无法为普通用户登录获得 user_id 和电子邮件的任何回显输出?
  • 总之,SQL匹配失败,还是匹配多行?养成在每次 SQL 调用后检查 SQL 中的错误(即如果 $r== false)的习惯。但还要回显系统正在采用的路线,并查看您的第一个查询中的 num_rows = 0 还是 2。
  • 返回多少行?它可能是 2(如果你有不可靠的测试数据)。它可以是 0(即使你认为它应该是 1)。这意味着你不会得到任何回报。我的意思是,它只是调试该部分以检查您认为应该发生的事情正在发生 - 您错过了错误检查,例如 SQL 失败或返回更多行(=unexpected)等
  • 我无法对输出进行任何错误检查。在执行 $number = mysqli_num_rows($r); 之前,我已经尝试过 mysqli_store_result()然后在 $_SESSION['first_name'] 旁边将其回显到问候语中。在查看页面源代码中也看不到任何有用的东西。
  • 去掉mysqli_query前的@,vardump($r); var_dump(mysqli_num_rows($r)) 等。看看会发生什么。如果您发现任何不妥之处,请将它们移除。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-07
  • 1970-01-01
  • 2018-10-31
  • 2018-03-26
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多