【问题标题】:Get session data codeigniter from non codeigniter project从非 codeigniter 项目获取会话数据 codeigniter
【发布时间】:2017-04-13 06:44:21
【问题描述】:

我通过单击按钮发送了用户 ID

<form action="https://www.mysite.co.uk/some/index.php" method="POST">
    <input type="hidden" name="userid" value="ZUpmenVaN0ZVTTBmejNGZGNwZGFha1NmR0tuSjdaT3VYdjV5cTF4WGtISzRvK0ptOC9vZmQyc3J3T3cwTmplbWZ3alhod0xMYUhlQ2xLSng4WWI4ZEE9PQ2">
    <input type="submit" value="Go" style="font-size:14px; padding:20px;">
</form>

然后我获取用户 ID 并在普通 php 页面中使用它(outsidecodeigniter)

www.mysite.co.uk/some/index.php

<?php 
session_start(); 

  <?php
    if (isset($_POST['userid'])) {
        $_SESSION['userid'] = $_POST['userid'];
        $userid = $_SESSION['userid'];
    }
    if (isset($_SESSION['userid'])) {
        echo "You are logged in and have access to these tests.";?>

code here

    <?php
    } else {
        header ('location: ../index.htm');
    }
    ?>

我现在需要在控制器中使用用户 ID,该控制器将数据发送到代码点火器中的数据库

ci 控制器

 private function getResults()
    {
        $this->load->library('session');
        $this->session->set_userdata('user_id', $_POST['userid']);
        $userID = $this->session->userdata('user_id');

        $score = $this->actions->getSentEmailCount();
        $percentilescore = $percentile = $this->actions->getPercentile($score);
        $testID = '134';

        {
            $data['emailcount'] = $score = $this->actions->getEmailCount();
            $data['sentEmailCount'] = $score = $this->actions->getSentEmailCount();
            $data['score'] = $score = $this->actions->getScore();
            $data['percentile'] = $percentile = $this->actions->getPercentile($score);
            $data['timespent'] = $this->input->get('time');
        };

        $adddata = array(
            'uniID' => '5',
            'testName' => 'Abintegro E-Tray test',
            'testID' => $testID,
            'total' => '20',
            'useInPercentile' => '1',
            'correct' => $score = $this->actions->getScore(),
            'percent' => $score = $this->actions->getScore(),
            'percentile' => $percentilescore,
            'dateTaken' => date('Y-m-d H:i:s'),
            'timeSpent' => $this->input->get('time'),
            'userID' => $userID
        );

        $this->actions->add_record($adddata);
        return $this->load->view('client/results', $data);
    }

我收到一个错误

消息:未定义的索引:用户 ID

有人能看出我为什么会变成 null 吗?

【问题讨论】:

  • 错误信息是什么?试试var_dump($_SESSION) 有吗?或者您的 this.php 无法正常工作。
  • get null,我在某处丢失了值
  • 可能,您正在重置会话的全局变量。
  • 嗯好的,谢谢。生病查看会话可能重新启动的代码
  • 在 CI 目录之外工作也没关系,$_SESSION 是一个全局变量,可以在任何地方访问。

标签: php codeigniter session


【解决方案1】:

首先,您必须使用 codeigniter 库启动会话 只需致电:

$this->load->library('session');

然后设置你的会话:

$this->session->set_userdata('user_id', $_POST['userid']);

然后在你的函数中访问会话变量,

private function getResults()
    {
        $score = $this->actions->getSentEmailCount();
        $userID = $this->session->userdata('user_id');
        $percentilescore = $percentile = $this->actions->getPercentile($score);
        $testID = '134';

        $adddata = array(
            'uniID' =>  $userID,
            'testName' => 'Abintegro E-Tray test',
            'testID' => $testID,
            'total' => '20',
            'useInPercentile' => '1',
            'correct' => $score = $this->actions->getScore(),
            'percent' => $score = $this->actions->getScore(),
            'percentile' => $percentilescore,
            'dateTaken' => date('Y-m-d H:i:s'),
            'timeSpent' => $this->input->get('time'),
            'userID' => $userID
        );
$this->actions->add_record($adddata);
        return $this->load->view('client/results', $data);
}

Refer here for more..

【讨论】:

  • 好的,把这些全部弹出到函数中?
  • 是的,它会起作用的。但更好的是你可以把 $this->load->library('session');在你的类构造函数中
  • hmm 不断收到消息:未定义的索引:用户 ID,请继续使用它
  • var_dump($this->session->userdata()) 的结果是什么; ?
  • ["user_id"]=> NULL 这很奇怪,因为什么时候使用我刚刚添加到我得到用户 ID 的问题中的新代码
【解决方案2】:

当您在自定义 PHP 中设置会话时,您无法从 CI 中检索它。您必须从自定义 PHP 中检索它。 喜欢::

session_start();
$userID = $_SESSION['userid'];

参考:: Session Helper

【讨论】:

  • 是的,请替换“$userID = $this->session->userdata('userid');”使用“session_start();$userID = $_SESSION[userid];”它会起作用的。
  • 谢谢,看起来很可爱很简单,请执行此操作并回复您。
  • 出现错误使用未定义的常量用户 ID - 假定为“用户 ID”,但尝试修复它然后接受您的回答
  • 首先检查您的会话值是否设置。 session_start(); var_dump($_SESSION);死;
  • 嗯,我什么也没得到。生病调试这个并回到这个答案。谢谢
猜你喜欢
  • 2016-07-03
  • 2016-05-28
  • 2014-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
相关资源
最近更新 更多