php - 无法跨页面获取某些 session 变量

标签 php mysql session-variables

尝试向我的 session 变量添加一些额外的元素以进行文件系统目录工作,我注意到我无法添加一些元素。这是我所拥有的:

    <?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 不起作用。提前致谢。

最佳答案

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')"; 

关于php - 无法跨页面获取某些 session 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10646613/

相关文章:

php - 强制 Firefox 进入标准合规模式

mysql - 获取数据库中存储的日期与当前日期的日期差

amazon-web-services - 在 AWS 负载均衡环境中使用经典 ASP session 变量是否有技巧?

php - 如何在php作业调度程序功能页面中添加$id

php - 如何使用 XSL 代码在 HTML 表格中显示图像?

php - 这部分代码卡住了我的电脑

mysql - 在mysql查询中获取东部标准时间

mysql - 是否有可以与 MySQL Community Server 5.5 一起使用的较新版本的 IQ 驱动程序?

session - ColdFusion 删除 session 变量

Session_End 事件中 Session 变量的 asp.net 值