我在codeigniter中遇到了这个问题,它完美地加载了index()页面,但当涉及到任何其他函数时,它只会给我一个404错误。这是一个骨架代码:
<?php if( ! defined('BASEPATH')) exit('No direct script access alloowed');
Class User extends CI_Controller{
var $err;
public function construct(){
parent::_construct();
session_start();
$this->load->database();
$this->load->model('User_Model');
}
public function index(){
if(isset($this->session->userdata['userID'])){
$this->welcome();
}else{
$this->load->view('banner','Login');
if(isset($this->err)){
$data["error"] = $this->err;
$this->load->view('login',$data);
}else{
$this->load->view('login');
}
$this->load->view('footer');
}
}
public function test(){
echo "Hello World";
}
public function welcome(){
$this->load->view('test');
}
public function register(){
$this->load->view('banner','Login');
if(isset($this->err)){
$data["error"] = $this->err;
$this->load->view('register',$data);
}else{
$this->load->view('register');
}
$this->load->view('footer');
}
public function registration(){
//registration ops
}
public function login(){
//login ops
}
}
?>
因此,当我试图导航到www.example.com/index.php/user时,它运行得很好,但当我试图让它运行register或进行登录操作时,它只会抛出404错误。有什么解决办法吗?
以下是使用with index()生成的html代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<link href="http://localhost/baseball/css/login.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div>
<p><img src="http://thumbs.dreamstime.com/z/cartoon-baseball-player-vector-illustration-30999087.jpg" height="250" alt=""/>Baseball Card Shop</p>
<hr />
<p class="loginInfo">
<a href="shopping cart">Shopping Cart</a>
</p>
<hr/>
</div>
<div class="central_login">
<div class="main">
<form action="http://localhost/baseball/index.php/user/login" method="post" accept-charset="utf-8"> <div class="left">
<p>
<label for="login"> Login: </label>
<input type="text" name="login" id="login" pattern="^[a-zA-Z0-9]+$" required/>
</p>
<p>
<label for="pwd">Password: </label>
<input type="password" name="pwd" id="pwd" required/>
</p>
<p><label id='pwdErr'></label>
<input type="submit" value="Log in" id="create-account" class="button"/>
</p>
</div>
</form> <div class="right">
<form action="http://localhost/baseball/index.php/user/register" method="post" accept-charset="utf-8"> <p>Not our customer, worry not ! Register here, it is free!</p>
<input type="submit" value="Register HERE!" id="register" class="button"/>
</form> </div>
</div>
</div>
<footer>
<p>CSC309 Assignment</p>
<p>Baseball Web Application</p>
</footer>
</body>
</html>
谢谢大家!
您需要更改.htaccess。以下.htaccess将帮助您:
RewriteEngine on
RewriteCond $1 !^(index'.php|resources|robots'.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
并且需要在config.php中更改以下配置:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
在您的代码中有几件事值得注意:
首先,var关键字的使用。它用于在php4中声明类成员。
它在php5及更高版本中已被弃用。
它会起作用的。但提出警告是不好的做法。
第二。从你发布的代码来看,我相信你还没有自动加载会话库。
将构造函数中的session_start()代码更改为:
$this->library->load('session');
我注意到的第三件事是这行代码:
$this->load->view('banner','Login');
loader类的视图函数采用以下格式的参数。
$this->load->view('file_name', $data, true/false);
您的语法可能不正确。
如果你计划加载横幅和登录文件,你应该这样做:
$this->load->view('banner');
$this->load->view('Login');
附带说明一下,使用本机php会话和CI会话可能会令人困惑,在某些情况下也可能引发冲突。