从非codeigniter项目获取会话数据codeigniter

时间:2016-11-29 10:06:55

标签: php codeigniter session

我通过点击按钮发送用户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');
    }
    ?>

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

中的数据库

ci controller

 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);
    }

我收到错误

  

消息:未定义的索引:userid

有人可以看到为什么我会变空吗?

2 个答案:

答案 0 :(得分:2)

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

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

参考:: Session Helper

答案 1 :(得分:0)

首先,您必须使用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..