无法跨页面获取某些会话变量

编程入门 行业动态 更新时间:2024-10-27 09:40:07
本文介绍了无法跨页面获取某些会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

Trying to add some extra elements to my session variables for filesystem directory work, and I noticed that I can't add some. Here's what I have:

<?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 plus the host name plus the current directory: $url = '' . $_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 不起作用.提前致谢.

Note: $_SESSION['first_name'] and $_SESSION['company_name'] have always worked correctly, however adding email and user_id is not working. Thanks in advance.

推荐答案

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

email and user_id will never work for the publisher: as the login function returns "pub_id" and "cemail". To fix this, you could change the SQL to:

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

更多推荐

无法跨页面获取某些会话变量

本文发布于:2023-10-11 22:19:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1482999.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   页面

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!