codeigniter-session登录错误

kfgdxczn  于 2021-06-24  发布在  Mysql
关注(0)|答案(3)|浏览(275)

嗨,我是编码的登录和注册模块,我发现了这样的会话错误。首先我登录然后我关闭页面。之后,我重新打开页面并放置路径url,这将发生。。。。。,
如果我放置路径url“localhost/codeigniter/index.php/some controller”,它将显示处于预登录状态的viewpage。
但是如果我单击某个东西来更改页面,它将在登录状态和路径url从“localhost/codeignite/index.php/some controller”更改为“[::1]/codeigniter/index.php/some controller”之后显示
因此,如果我尝试在路径url中首先放置[::1]而不是localhost,它也会显示after login状态。发生什么事了?为什么我不能使用localhost?如果我使用localhost,它似乎没有会话。或者这是xampp mysql上的一个bug??

<?php   ///////////////view/header.php///////////
                if($this->session->userdata('is_logged_in'))
                {
                    echo "Welcome!, " . $this->session->userdata('username');
                    echo  anchor('login/logout','Sign Out') ."</span>";
                }
                else
                {
                    echo anchor('login','Log in');
                    echo anchor('register/index','Register') ;
                }
            ?>

登录控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    class Login extends CI_Controller
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->library(array('form_validation','session'));
        }
        public function index()
        {

            $this->load->view('header');

            $this->load->view('regislogin/loginform');

            $this->load->view('footer');
        }

        public function login_validation()
        {
            $this->load->view('header');

            $this->form_validation->set_rules('username','Username','required|trim|callback_validate_credentials');
            $this->form_validation->set_rules('password','Password','required|md5|trim');

            if($this->form_validation->run()) //==TRUE
            {
                $data = array(
                    'username' => $this->input->post('username'),
                    'is_logged_in' => true
                );
                $this->session->set_userdata($data);
                redirect('login/userPage');     
            }
            else
            {
                $this->load->view('regislogin/loginform');
            }

        }

        public function userPage() 
        {
            if($this->session->userdata('is_logged_in')) //if==true
            {
                $this->load->view('header');
                $this->load->view('regislogin/success');

            }
            else
            {

            }           
        }

        public function validate_credentials()
        {
            //$this->load->model('login_model');  //autoload already
            if($this->login_model->can_login()) //if can_login == true
            {
                return true;
            }
            else
            {
                $this->form_validation->set_message('validate_credentials','Username/Password Incorrect');
                return false;
            }
        }

        public function logout()
        {
            $this->session->sess_destroy();
            redirect('login');
        }
    }

登录模型

<?php
class Login_model extends CI_Model
{
    public function can_login()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password'))); 

        $query = $this->db->get('member');

        if($query->num_rows() == 1)  //if Found 1 match
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

寄存器控制器

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library(array('form_validation','session'));
    }
    public function index()
    {   
        $this->load->view('header');
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[6]);

        if($this->form_validation->run() == FALSE)
        {
            $this->load->view('regislogin/register_view');
        }
        else
        {
            $key = md5(uniqid());
            $this->load->library('email', array('mailtype'=>'html'));//default mailtype is 'text'
            $this->email->from('supanat_thana@hotmail.com', "Webmaster");
            $this->email->to($this->input->post('email'));
            $this->email->subject("Confirm your account");
            $message = "<p>Thank you for register!</p>";
            $message .= "<p><a href='".base_url()."register/register_activate/$key'>Click Here</a> เพื่อยืนยันบัญ๙ีของคุณ </p>";
            $this->email->message($message);

            if($this->register_model->add_temp_user($key))
            {
                if($this->email->send())
                {
                    $data['emailstr'] = "Please activate your account by email";
                } 
                else
                {
                    $data['emailfail'] = "could not send the email.";
                }
            }
            else
            {
                echo "Problem: cannot insert to out database";
            }

            $this->load->view('regislogin/success', $data);
        }

    }

    public function register_activate($key)
    {
        if($this->register_model->is_key_valid($key))
        {
            if($newuser = $this->register_model->addMember($key))
            { 

                $data = array(
                    'username' => $newuser,
                    'is_logged_in' => true
                );
                $this->session->set_userdata($data);
                redirect('login/userPage');

                echo "successfully activate account <br/>";
                echo anchor('home', 'back to Home');
            } else echo "sorry cannot activate account";
        }
        else
        {
            echo "invalid key";
        }
    }

}

寄存器型号

<?php
class Register_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    public function add_temp_user($key)
    {
        $data = array(
            'username' => $this->input->post('username'),
            'password' => md5($this->input->post('password')),
            'email' => $this->input->post('email'),
            'phone' => $this->input->post('phone'),
            'key' => $key
        );

        $query = $this->db->insert('temp_user', $data);
        if($query) {return true;}
        else {return false;}
    }

    public function is_key_valid($key)
    {
        $this->db->where('key', $key);
        $query = $this->db->get('temp_user');

        if($query->num_rows() == 1)
        {
            return true;
        }
        else{return false;}
    }

    public function addMember($key)
    {
        $this->db->where('key', $key);
        $temp_users = $this->db->get('temp_user');

        if($temp_users)
        {
            $row = $temp_users->row();

            $data = array(
                'username' => $row->username,
                'password' => $row->password,
                'email' => $row->email,
                'phone' => $row->phone
            );

            $did_adduser = $this->db->insert('member', $data);
        }
        if($did_adduser)
        {
            $this->db->where('key', $key);
            $this->db->delete('temp_user');
            return $data['username'];
        } else{return false;}
    }

    public function isUserExist()
    {
        $username = $this->input->post('username');
        $this->db->where('username',$username);
        $query = $this->db->get('member');
        if($query->num_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public function isEmailExist()
    {
        $email = $this->input->post('email');
        $this->db->where('email',$email);
        $query = $this->db->get('member','temp_user');
        if($query->num_rows() > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

查看/regislogin/success.php

<DOCTYPE! html>
<html>
<head>
    <title> Register </title>
    <meta charset="utf-8">
</head>
<body>
<?php
    if($this->input->post("btn"))
    {
        echo "<h3>Thank you for your registration</h3>";
        echo $emailstr."<br/><br/>";
        if(isset($emailfail)) {echo $emailfail."<br/><br/>";} else{}
        echo anchor('home', 'กลับสู่หน้าหลัก');

    }
    else
    {
        echo "<h3>Successful Login</h3>";
        echo "<pre>";
        echo "Welcome!, ". $this->session->userdata('username');
        echo "</pre>";

        echo anchor('login/logout', 'Logout')."<br/>"; 
        echo anchor('home', 'Back to Home');
    }
?>
</body>
</html>

查看/loginregis/loginform.php

<div class="loginbox">
    <?php echo form_open('login/login_validation');?>
        <h2> Login </h2>
        <ul id='login'>
            <li>
                Username: <br>
                <input type='text' name='username'>
            </li>
            <li>
                Password:<br>
                <input type='password' name='password'>
            </li>
            <br>
            <li>
                <input type='submit' name='btn2' value='Log in'>
            </li>
            <li>
                <br>
                <?php echo anchor("register/index","Register");?>
            </li>
        </ul>   
    <?php echo form_close();?>
</div>

view/regislogin/register\u view.php

<head>
    <meta charset="utf-8">
    <title>Register</title>
</head>

    <div id="wrapper_regis">
        <div id="header_regis">
        <h1>Register</h1>
        </div>

    <?php echo form_open('register');?>
        <table>
            <tr>
                <td>Username : </td>
                <td><input type="text" name="username"  value=""/></td>
            </tr> 
            <tr>
                <td>Password : </td>
                <td><input type="password" name="password" value=""/></td>

            </tr>
            <tr>
                <td>Confirmed Password : </td>
                <td><input type="password" name="pass_confirm" value=""/></td>
            </tr>
            <tr>
                <td>Email : </td>
                <td><input type="text" name="email" value=""/></td>
            </tr>

            <tr>
                <td>Phone : </td>
                <td><input type="text" name="phone"  value=""/></td>

            </tr>
        </table>
        <br/>
        <input class="btn" type="submit" name="btn" value="สมัครสมาชิก"/>

    <?php echo form_close();?>
    </div>

mysql表

CREATE TABLE `temp_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(35) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(50) NOT NULL,
  `phone` varchar(15) NOT NULL,
  `key` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `member` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(30) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(50) NOT NULL,
  `phone` varchar(15) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

在config/autoload.php

$autoload['libraries'] = array('database', 'session');
$autoload['helper'] = array('url','form');
$autoload['model'] = array('register_model','login_model');
i2byvkas

i2byvkas1#

我导入你的代码,它是工作,因为它应该是。请做下面的事情1。添加一个空数组$data=array();索引函数中的register.php(控制器)内部。2.在函数userpage内的login controller中,else condition在login page上添加一行重定向它

public function userPage() 
    {
        if($this->session->userdata('is_logged_in')) //if==true
        {
            $this->load->view('header');
            $this->load->view('regislogin/success');

        }
        else
        {
            redirect('login');
        }           
    }

如果您有任何问题,请在出现错误的地方分享您的页面名称。

8ljdwjyq

8ljdwjyq2#

请检查您的会话是否正常工作,以及您是否在控制器和注销上添加了适当的条件。最好在hooks中添加一个条件,这样就不必为每个控制器都编写它。还可以提供一些代码片段来查看更多信息。

rxztt3cl

rxztt3cl3#

好吧,我来解决。在文件夹路径config/config.php中设置$config['base\u url']='http://'。$\u server['http\u host']./foldername';

相关问题