[eluser]bhenbe[/eluser]
Hi all,
for an unknown reason, a redirect in a pre_system hook doesn't work. It's the first time i try this.
I need to redirect some urls if they are listed in a database. So, something like
http://www.mysite.com/code-igniter can be a redirect and not a controller.
Here's my code :
Config hook :
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------------
| Hooks
| -------------------------------------------------------------------------
| This file lets you define "hooks" to extend CI without hacking the core
| files. Please see the user guide for info:
|
| http://ellislab.com/codeigniter/user-guide/general/hooks.html
|
*/
$hook['pre_system'] = array(
'class' => 'Direct_access',
'function' => 'get_direct_access',
'filename' => 'direct_access.php',
'filepath' => 'hooks',
'params' => array()
);
/* End of file hooks.php */
/* Location: ./application/config/hooks.php */
Hook Direct_access :
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Direct_access {
function __construct() {
}
function get_direct_access() {
/*** Step 1 - Get URL ***/
$url = $_SERVER["REQUEST_URI"];
$url = explode('/', $url);
if ((!is_array($url)) || (count($url) < 2))
return;
$url = array_pop($url);
/*** Step 2 - Check URL ***/
if (preg_match("/^([-a-z0-9_-])+$/i", $url) == false)
return;
/*** Step 3 - Check DB ***/
$link = @mysql_connect('gna', 'gna', 'gna');
if (!$link)
return;
$db_selected = @mysql_select_db('gna', $link);
if (!$db_selected)
return;
$query = sprintf("SELECT `redirect` FROM `direct_access` WHERE `url` LIKE '%s'", mysql_real_escape_string($url));
$result = mysql_query($query);
if (!$result)
return;
$row = mysql_fetch_assoc($result);
Header('Location: '.$row['redirect']);
return;
}
}
/* End of file direct_access.php */
/* Location: ./application/hooks/direct_access.php */
I changed the return code by an echo to check each part of my code. Nothing's wrong but the Header function doesn't work. CI always show me a 404 page because the controller doesn't exist.
Anyone have an idea ?
Thanks !