Php 使用codeigniter成功登录后如何显示主页?
我尝试了Php 使用codeigniter成功登录后如何显示主页?,php,codeigniter,Php,Codeigniter,我尝试了redirect()和$this->load->view('target_page')但是没有成功,所以请帮助我: 我的控制器在这里: class Login_control extends CI_Controller { public function index() { $this->load->model('login_model'); $this->load->helper('url'); if(isset($_POST['Logusername
redirect()
和$this->load->view('target_page')
但是没有成功,所以请帮助我:
我的控制器在这里:
class Login_control extends CI_Controller {
public function index() {
$this->load->model('login_model');
$this->load->helper('url');
if(isset($_POST['Logusername'])|| isset($_POST['Logpassword']))
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data > 0 )
{
echo '<font color="#00FF00">'. "OK".'</font>';
$this->load->view('testing',$data);
}
else
{ echo '<font color="#FF0000">'. "Login Failed ! Username or Password is Incorrect.".'</font>' ;
}
exit();
}
$this->load->view('signup_view');
}
}
类登录\u控件扩展CI\u控制器{
公共职能指数(){
$this->load->model('login_model');
$this->load->helper('url');
如果(isset($_POST['Logusername'])| | isset($_POST['Logpassword']))
{
$user=$\u POST['Logusername'];
$pass=$_POST['Logpassword'];
$data=$this->login\u model->login1($user,$pass);
如果($data>0)
{
回音“好的”;
$this->load->view('testing',$data);
}
其他的
{echo''。登录失败!用户名或密码不正确。“”;
}
退出();
}
$this->load->view('signup_view');
}
}
试试这段代码。您在重定向之前编写了echo
,因此,它可能无法工作
class Login_control extends CI_Controller{
public function index()
{
$this->load->model('login_model');
$this->load->helper('url');
if(isset($_POST['Logusername'])|| isset($_POST['Logpassword']))
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data > 0 )
{
redirect('testing');
}
else
{ echo '<font color="#FF0000">'. "Login Failed ! Username or Password is Incorrect.".'</font>' ;
}
exit();
}
$this->load->view('signup_view');
}
}
类登录\u控件扩展CI\u控制器{
公共职能指数()
{
$this->load->model('login_model');
$this->load->helper('url');
如果(isset($_POST['Logusername'])| | isset($_POST['Logpassword']))
{
$user=$\u POST['Logusername'];
$pass=$_POST['Logpassword'];
$data=$this->login\u model->login1($user,$pass);
如果($data>0)
{
重定向(“测试”);
}
其他的
{echo''。登录失败!用户名或密码不正确。“”;
}
退出();
}
$this->load->view('signup_view');
}
}
试试这个
在控制器中
class Login_control extends CI_Controller{
public function index()
{
$this->load->model('login_model');
$this->load->helper('url');
if(isset($_POST['Logusername']) && isset($_POST['Logpassword'])) # Change || to &&
{
$user = $_POST['Logusername'];
$pass = $_POST['Logpassword'];
$data = $this->login_model->login1($user,$pass);
if($data == 1){ # check is there only one user
echo '<font color="#00FF00">OK</font>';
$this->load->view('testing',$data);
}
else{
echo '<font color="#FF0000">Login Failed ! Username or Password is Incorrect.</font>' ;
}
}
$this->load->view('signup_view');
}
}
当您在codeigniter中开发时,更好地使用它的内置方法来获取和发布数据。对于身份验证,您可以创建一个库,该库将自动加载,并检查类似于userid的会话,如果找不到,则将用户重定向到登录页面。您可以在该库中创建一个数组,该数组将定义公共/经过身份验证的页面,在此基础上禁止用户访问经过身份验证的页面。您可以为控制器尝试以下操作:
class Login_control extends CI_Controller
{
public function index()
{
$this->load->model('login_model');
$this->load->helper('url');
$Logusername = $this->input->post('Logusername', true);
$Logpassword = $this->input->post('Logpassword', true);
if (!empty($Logusername) && !empty($Logpassword)) {
$user = $Logusername;
$pass = $Logpassword;
$data = $this->login_model->authenticate($user, $pass);
if ($data == TRUE) {
/* User this flashdata to display the message after redirect */
$this->session->set_flashdata('success', 'Logged in successfully');
redirect(site_url("dashboard"));
} else {
/* User this flashdata to display the message after redirect */
$this->session->set_flashdata('success', 'Wrong Username/password');
redirect(site_url());
}
}
$this->load->view('signup_view');
}
}
模型
public function authenticate($Logusername, $Logpassword)
{
$select_col = "iUserId";
$where = "`vUserName` =?"; //*
$where.=" AND BINARY `vPassword`=?";
$sql = "SELECT " . $select_col . " FROM user WHERE " . $where . "";
$result = $this->db->query($sql, array($Logusername, $Logpassword))->result_array();
if (is_array($result) && count($result) > 0) {
/* Create Session and store userid */
$this->session->set_userdata("iUserId", $result[0]["iUserId"]);
return TRUE;
} else {
return FALSE;
}
}
有许多方法可以对用户进行身份验证。检查Codeigniter中的挂钩也发布您的型号代码!!将| |更改为&&here
if(isset($_POST['Logusername'))&&isset($_POST['Logpassword']))
对于身份验证,您最好使用一些现成的身份验证库,如Codeigniter有一个表单验证库。您还可以在表单验证成功部分设置会话数据。尝试解释为什么您的代码也可以工作echo“OK”有什么用代码>@Saty我不明白你只是解释为什么你的代码为OP工作。你的代码有什么变化done@user2848909检查此项谢谢您的回复Abdulla@user2848909将测试
替换为有效的URL。是!它成功地重定向了,但我在代码中包含了一些ajax和jquery,所以“测试”页面内容显示在同一页面上提交按钮的底部!如何将其重定向到ajax之外?您的整个登录过程必须基于ajax,并且在成功登录后,您只需使用ajax中返回的html内容替换div。
public function authenticate($Logusername, $Logpassword)
{
$select_col = "iUserId";
$where = "`vUserName` =?"; //*
$where.=" AND BINARY `vPassword`=?";
$sql = "SELECT " . $select_col . " FROM user WHERE " . $where . "";
$result = $this->db->query($sql, array($Logusername, $Logpassword))->result_array();
if (is_array($result) && count($result) > 0) {
/* Create Session and store userid */
$this->session->set_userdata("iUserId", $result[0]["iUserId"]);
return TRUE;
} else {
return FALSE;
}
}