Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在其他页面上使用会话变量_Php_Wordpress_Session - Fatal编程技术网

Php 在其他页面上使用会话变量

Php 在其他页面上使用会话变量,php,wordpress,session,Php,Wordpress,Session,这个问题可能有点愚蠢,但我只是从会话开始我的冒险-以前不需要使用它们。在主页上,我有一个适当存储所有变量的会话,这没有问题 当我转到同一域下的子页并尝试从会话调用变量时,我只得到空字段。我尝试了打印(请求$);在子页面上,打印出以下内容: Array ( [wp-settings-1] => libraryContent=browse&editor=html [wp-settings-time-1] => 1478015951 [PHPSESSID] => 0744bf

这个问题可能有点愚蠢,但我只是从会话开始我的冒险-以前不需要使用它们。在主页上,我有一个适当存储所有变量的会话,这没有问题

当我转到同一域下的子页并尝试从会话调用变量时,我只得到空字段。我尝试了打印(请求$);在子页面上,打印出以下内容:

Array ( [wp-settings-1] => libraryContent=browse&editor=html [wp-settings-time-1] => 1478015951 [PHPSESSID] => 0744bf21ab3712e4735e07d926433aa3 [sec_session_id] => 60e56049f51c76e9ec4932c6702e7b72 )
与主页上的输出相匹配。我知道我应该做一个会话_start();但是,当我在代码中包含这些内容时,会出现以下错误:

Notice: A session had already been started - ignoring session_start()
我知道变量没有被传递的原因是因为

if(isset($_SESSION["user_id"])){
$user_id = $_SESSION["user_id"];
}

echo "User id: " .$_SESSION["user_id"];
我只有

User id:
每当我尝试调用变量时,我都会得到一个错误,即它没有被设置

有没有我错过的一步

设置会话变量:

if ($stmt->num_rows == 1) {
        // If the user exists we check if the account is locked
        // from too many login attempts 
        if (checkbrute($user_id, $mysqli) == true) {
            // Account is locked 
            // Send an email to user saying their account is locked 
            return false;
        } else {
            // Check if the password in the database matches 
            // the password the user submitted.
            if ($db_password == $password) {
                // Password is correct!
                // Get the user-agent string of the user.
                $user_browser = $_SERVER['HTTP_USER_AGENT'];

                // XSS protection as we might print this value
                $user_id = preg_replace("/[^0-9]+/", "", $user_id);
                $_SESSION['user_id'] = $user_id;
                $sessions['user_id'] = $user_id;
                // XSS protection as we might print this value
                $username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username);

                $_SESSION['username'] = $username;
                $_SESSION['login_string'] = hash('sha512', $password . $user_browser);

                // Login successful. 
                return true;
            } else {
                // Password is not correct 
                // We record this attempt in the database 
                $now = time();
                if (!$mysqli->query("INSERT INTO login_attempts(user_id, time) 
                                VALUES ('$user_id', '$now')")) {
                    header("Location: ../error.php?err=Database error: login_attempts");
                    exit();
                }

                return false;
            }
        }
    } else {
        // No user exists. 
        return false;
以及启动安全会话的功能:

function sec_session_start() {
$session_name = 'sec_session_id';   // Set a custom session name 
$secure = SECURE;

// This stops JavaScript being able to access the session id.
$httponly = true;

// Forces sessions to only use cookies.
if (ini_set('session.use_only_cookies', 1) === FALSE) {
    header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
    exit();
}

// Gets current cookies params.
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"], $cookieParams["path"], $cookieParams["domain"], $secure, $httponly);

// Sets the session name to the one set above.
session_name($session_name);

session_start();            // Start the PHP session 
session_regenerate_id();    // regenerated the session, delete the old one. 
}

看到你的代码我知道这是wordpress网站

如果是,请执行以下步骤:

步骤1:将以下代码添加到wp content/themes文件夹/functions.php中

add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');

function myStartSession() {
    if(!session_id()) {
        session_start();
    }
}

function myEndSession() {
    session_destroy ();
}
步骤2:设置用户id

$_SESSION['user_id'] = "your id";
步骤3:访问会话id

if(isset($_SESSION['user_id'])) {
    $value = $_SESSION['myKey'];
} else {
    $value = '';
}
如果你的网站不是wordpress

add session_start() at header.php(or first line of your code)
then use your session variables.

在何处将用户id设置为会话?如果是wordpress,则使用全局$session;打印(会话);它将打印会话变量。要将用户id设置为会话--$sessions['user\u id']='your user id';您可以访问应用程序中的任何位置否我在登录平台时在php会话中设置用户id,与wordpress$\u会话['user\u id']=$user\u id无关;这不是wordpress网站。这是一个php网站,wordpress会话可能是因为服务器上安装了wordpress,但与我使用atm的实际站点无关。请查看我编辑的问题,了解我如何设置和启动会话以及添加会话_start();在文件的开头抛出一个错误,指出有一个会话已在运行