session_start();
if(isset($_SESSION['id'])){
$username = bin2hex($_SESSION['id']);
}else{
echo "<script> window.location.replace('Login.php') </script>";
}
登录页面
session_start();
$_SESSION['id'] = $userunhex;
答案 0 :(得分:2)
&#34;它将我发回登录页面。&#34;
这是因为会话数组(显然)为空并且尚未分配给任何内容。
另外,您需要在第二个文件中切换会话数组和变量,即登录页面/文件。
(经测试为真)
<强> File1.php 强>
<?php
session_start();
$_SESSION['id'] = 123; // My own test assignment. You need to use the one you are using.
if(isset($_SESSION['id'])){
// This is actually valid syntax.
// echo $username= bin2hex($_SESSION['id']);
$username = bin2hex($_SESSION['id']);
}else{
// echo "Not set";
echo "<script> window.location.replace('Login.php') </script>";
}
File2.php 并检查会话是否也已设置。
将$_SESSION['id'] = $userunhex;
更改为$userunhex = $_SESSION['id'];
<?php
session_start();
if(isset($_SESSION['id'])){
$userunhex = $_SESSION['id'];
echo $userunhex;
}
else {
echo "Session is not set".
}
答案 1 :(得分:0)
这样做了:
session_start();
$username = $_SESSION['id'];
$username = bin2hex($username);
if(isset($username)){
}else{
echo "<script> window.location.replace('Login.php') </script>";
}