Welcome Guest, Not a member yet? Register   Sign In
How can i do a redirect pre-systme hook ?
#1

[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 !
#2

[eluser]toopay[/eluser]
Change
Code:
Header('Location: '.$row['redirect']);

return;
above, into :
Code:
header("Location: $row['redirect']");

exit;
#3

[eluser]bhenbe[/eluser]
Thanks !

You must also check if $row doesn't return false. Without this check, exit will stop the controller calls.

Code:
$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);
    
    if (!$row)
     return;

  Header('Location: '.$row['redirect']);

  exit;





Theme © iAndrew 2016 - Forum software by © MyBB