drf16571 2015-11-01 21:11
浏览 46
已采纳

我无法在第二个PHP文件中检索会话变量

What I am trying to do is, I have an HTML page that is an admin login form. I want to pass the username to other pages to verify that the user is logged in before being allowed to access the functions of the called page.

I have this code in an PHP file that is included in an .html file:

session_start();
session_register('username');
$_SESSION['id']=10;
echo "Login session id is " .$_SESSION['id'];

unset($_SESSION['username']);
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{
    require_once 'config.inc.php';

    $connect = mysqli_connect($host_name, $user_name, $password, $database);

    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    else
    {
        $uName = $_POST['name'];
        $_SESSION['username'] = $uName;
        echo $_SESSION['username'];

..... > more to the file but this displays the username just fine.

In the second file, a .html file with PHP inline in the file:

<?php

        session_start();

        echo "Add user session id is ";
        echo "<br>";
        echo $_SESSION['id'];
        echo "<br>";
        echo "Hello";
        echo "<br>";
        echo $_SESSION['username'];
        echo "<br>";
        echo $_SESSION;
        echo "<br>";
        echo $_SESSION['time'];
        echo "<br>";
        echo date('Y m d H:i:s', $_SESSION['time']);

The hard code text and the "echo date('Y m d H:i:s', $_SESSION['time']);" display fine. The line echo date('Y m d H:i:s', $_SESSION['time']); displays the wort Array.

Can anyone tell me why I can't get the value of username?

  • 写回答

2条回答 默认 最新

  • dongru2019 2015-11-01 21:15
    关注

    Your issue is as you said yourself,

    In the second file, a .html file with PHP inline in the file:

    PHP has to be in a .php file, not a .html file extension.

    Edit 1:

    As per PHP.net:

    Warning This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

    You should stop using it.

    Edit 2:

    You are also echoing $_SESSION,

    echo $_SESSION;
    

    that should be throwing an array to string conversion error if your error reporting is enabled and the file extension is .php.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 java代码写在记事本上后在cmd上运行时无报错但又没生成文件
  • ¥15 关于#python#的问题:在跑ldsc数据整理的时候一直抱这种错误,要么--out识别不了参数,要么--merge-alleles识别不了参数(操作系统-linux)
  • ¥15 PPOCRLabel
  • ¥15 混合键合键合机对准标识
  • ¥100 现在不懂的是如何将当前的相机中的照片,作为纹理贴图,映射到扫描出的模型上
  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡