会话值仅在单个页面中的页面刷新时设置

时间:2015-03-09 06:10:48

标签: php codeigniter smarty

当用户登录时,会话设置并显示正确的数据。但是当我点击“aboutview”,它有一个名为“aboutView()”的函数时,会显示以前登录的用户数据。当我刷新页面时,它将显示当前登录用户的正确数据。

请检查并更正错误。

这是我的控制器(user.php)

<?php
class User extends CI_Controller 
{
    public function __construct() 
    {
        parent::__construct();
        session_cache_limiter('private_no_expire');
        session_start();
        $this->load->model('usermodel');
        $this->load->helper(array('form', 'url', 'html', 'date'));
        $this->load->library(array('session', 'javascript', 'form_validation', 'upload','email'));
        $this->load->database();
        $url  = base_url() . 'user';
        $this->smarty->assign('url', "$url");
}

public function loginValid() 
    {
        $this->form_validation->set_rules('uname', 'Username', 'trim|required|min_length[3]');
        $this->form_validation->set_rules('pswd', 'Password', 'trim|required');
        if ($this->form_validation->run() == FALSE) 
        {
            $this->smarty->display('user/login.tpl');
        }   
        else
        {
            $this->load->model('usermodel');
            $res    = $this->usermodel->login();
            if ($res == "success") 
            {   
                $dataArray          = $this->usermodel->viewStatus();
                $notificationCount  = $this->usermodel->getNotification();

                $decode     = json_decode(json_encode($dataArray), TRUE);
                $getCount   = count(json_decode(json_encode($notificationCount),TRUE));
                $getName    = json_decode(json_encode($notificationCount),TRUE);

                $this->smarty->assign('count', $getCount );
                $this->smarty->assign('items', $decode);
                $this->smarty->assign('name', $getName);
                $this->smarty->assign('smsg', 'Login success');
                $this->smarty->display('user/home.tpl');
            } 
            else 
            {
                $this->smarty->assign('msg', 'Invalid username and password');
                $this->smarty->display('user/login.tpl');
            }
        }
    }

public function aboutView()
    {

        $dataArray  = $this->usermodel->aboutView();
        $decode     = json_decode(json_encode($dataArray),TRUE);
        $this->smarty->assign('items', $decode);
        $this->smarty->display('user/aboutview.tpl');
    }

这是我的模型“usermodel.php”

<?php

class Usermodel extends CI_Model 
{
    public function __construct() 
    {
        parent::__construct();
    }

 public function login() 
    {
        $username   = $this->input->post('username');
        $pswd       = $this->input->post('pswd');
        $this->db->select('username,userID');
        $data   = $this->db->get_where('users', array('userName' => $username, 'password' => $pswd))->row_array();
        if ($data) 
        {
            $this->session->set_userdata($data);
            return "success";
        }
        else 
            return "fail";
    }

 public function aboutView()
    {
        $id   = $this->session->userdata('userID');

        $this->db->select('*'); 
        $this->db->from('users');
        $this->db->where('userID', $id);
        $query  = $this->db->get();
        return $query->result();
    }

这是我的观点“aboutview.tpl”

<!DOCTYPE html>
<html>
    <head>
        <title>Profile</title>
        <link rel="stylesheet" type="text/css" href="http://localhost/codeig_smarty/css/style.css"/>
    </head>
    <body>
        <div class="coverPic">
            <img src="http://localhost/codeig_smarty/images/coverpicavatar.jpg" width="1259px" height="289px"/>
            <a href="http://localhost/codeig_smarty/index.php/user/fileUpload" class="profilePicLink">Upload new profile photo</a>
        </div>
        <div class="profilePic">
            <img src="http://localhost/codeig_smarty/images/avatar2.png" width="138px" height="163px"/>
            <a href="#" class="coverPicLink">Upload cover pic</a>
        </div>
        <div class="profileName"><p>{$items.0.fullName}</p></div>
        <div class="icons">
            <ul>
                <li><a href="#">Timeline</a></li>
                <li><a href="#">About</a></li>
                <li><a href="#">Friends</a></li>
                <li><a href="#">Photos</a></li>
                <li><a href="{$url}/displayMain">Home</a></li>
            </ul>
        </div>
        <div class="logout">
            <a href="http://localhost/codeig_smarty/index.php/user/logout">Logout</a>
        </div>
        <div class="about">
            <span>About</span>
        </div>
        <div class="editabout">
            <span><a href="http://localhost/codeig_smarty/index.php/user/aboutEdit">Edit</a></span>
        </div>
         <form name="aboutviewform" method="post" action="" onsubmit="">
            <table class="abouttable" >
                <tr>
                    <th>Fullname:</th>
                    <td>{$items.0.fullName}</td>
                </tr>
                <tr>
                    <th>Username:</th>
                    <td>{$items.0.userName}</td>
                </tr>
                <tr>
                    <th>Email:</th>
                    <td>{$items.0.email}</td>
                </tr>
                <tr>
                    <th>Date of birth:</th>
                    <td>{$items.0.dateofBirth}</td>
                </tr>
                <tr>
                    <th>Gender:</th>
                    <td><input type="radio" name="gender" value="male" checked="male"/>Male
                        <input type="radio" name="gender" value="female"/>Female
                    </td>
                </tr>
                <tr>
                    <th>Work:</th>
                    <td>{$items.0.work}</td>
                </tr>
                 <tr>
                    <th>Company Name:</th>
                    <td>{$items.0.companyName}</td>
                </tr>
                 <tr>
                    <th>Designation:</th>
                    <td>{$items.0.designation}</td>
                </tr>
                <tr>
                    <th>Education:</th>
                    <td>{$items.0.education}</td>
                </tr>
                <tr>
                    <th>Country:</th>
                    <td><select name="country">
                            <option value="india">India</option>
                            <option value="usa">USA</option>
                            <option value="uk">UK</option>
                            <option value="uae">UAE</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>State:</th>
                    <td><select name="state">
                            <option value="kerala">Kerala</option>
                            <option value="karnataka">Karnataka</option>
                            <option value="tamilnadu">Tamilnadu</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <th>Maritial status:</th>
                    <td><input type="radio" name="maritialstatus" value="unmarried" checked="unmarried"/>Unmarried
                        <input type="radio" name="maritialstatus" value="married"/>Married
                        <input type="radio" name="maritialstatus" value="divorced"/>Divorced
                    </td>
                </tr>
                <tr>
                    <th>Current Place:</th>
                    <td>{$items.0.currentPlace}</td>
                </tr>
                <tr>
                    <th>Contact number:</th>
                    <td>{$items.0.contactNum}</td>
                </tr>
                <tr>
                    <th>College:</th>
                    <td>{$items.0.college}</td>
                </tr>
                <tr>
                    <th>Highschool:</th>
                    <td>{$items.0.highSchool}</td>
                </tr>
                <tr>
                    <th>Status:</th>
                    <td><input type="radio" name="status" value="{$items.0.status}" checked="active"/>Active
                        <input type="radio" name="status" value="{$items.0.status}"/>Inactive
                    </td>
                </tr>
            </table>
        </form>
    </body>
</html>

0 个答案:

没有答案