【发布时间】:2015-05-15 13:49:23
【问题描述】:
我想通过 www.siteurl.com/user/username 而不是 www.siteurl.com/user?name=username
获取用户的个人资料这在 localhost 中完美运行,但在 live 服务器中却不行 它只是给出一个 404 Page Not Found,但在 localhost 中它指向请求的 url。如何在实时服务器中执行此操作?
这是我的代码
在config.php 中启用了钩子
$config['enable_hooks'] = TRUE;
然后创建一个 hooks.php 在 application->config 中添加以下代码
$hook=array(
'pre_system' => array(
array(
'class' => 'Userlookup',
'function' => 'check_uri',
'filename' => 'Userlookup.php',
'filepath' => 'hooks',
'params' => NULL,
),
),
);
然后,在 application-> 钩子中创建它包含的 Userlookup.php,
<?php defined('BASEPATH') or die('No direct script access allowed');
class Userlookup{
protected $uri_username;
protected $connection_method;
protected $hostname;
protected $username;
protected $password;
protected $database;
public function __construct()
{
// Configure database connection
include(APPPATH.'config/database'.EXT);
$this->hostname = $db[$active_group]['hostname'];
$this->username = $db[$active_group]['username'];
$this->password = $db[$active_group]['password'];
$this->database = $db[$active_group]['database'];
}
public function check_uri()
{
// First, we need get the uri segment to inspect
$request_uri = explode('/',substr($_SERVER['REQUEST_URI'], 1)); //$request_uri[0] == username
$this->uri_username = array_shift($request_uri); //string 'socialsite' (length=10)
$connection_router = array_shift($request_uri);
$this->connection_method = empty($connection_router) ? 'index' : $connection_router;
// Connect to database, and check the user table
mysql_connect($this->hostname, $this->username, $this->password) AND mysql_select_db($this->database);
$res = mysql_query("SELECT user_Id FROM tbl_user WHERE user_name='".$request_uri[0]."'");
// $id = mysql_result("SELECT user_Id FROM tbl_user WHERE user_name='".$request_uri[0]."'");
// var_dump($id,0);die;
// print_r($id);die;
if ($row = mysql_fetch_object($res))
{
// If, there is a result, then we should modify server data
// Below line means, we told CodeIgniter to load
// 'User' controller on 'index', 'info' or any valid connection/method and we send 'id' parameter
//$_SERVER['REQUEST_URI'] = '/user';
$id = mysql_result($res,0);
//print_r($id);die;
$_SERVER['REQUEST_URI'] = '/user?id='.$id;
//var_dump($_SERVER['REQUEST_URI']);die;
}
mysql_free_result($res);
}
}
这是我的 .htaccess 文件
RewriteEngine On
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
【问题讨论】:
-
您在共享主机中吗?你能gotoanswer.stanford.edu/?q=Codeigniter+redirect+not+working在那里检查一些吗,如果你能阅读服务器错误日志,它也会对你有所帮助
-
not working是什么意思,请说明任何错误或其行为。 -
它给出 404 错误“找不到您请求的页面。”
-
我认为问题在于你定义钩子使用的方式: $hook['pre_system'][] => array( 'class' => 'Userlookup', 'function' => 'check_uri' , 'filename' => 'Userlookup.php', 'filepath' => 'hooks', 'params' => NULL, ), );
-
我认为你应该专注于你的 htaccess 和路由来解决这个问题。
标签: php codeigniter