[eluser]calumogg[/eluser]
Hi I have been working on a routing script that will check if a username exists in a database if it does the script will redirect to the users profile or requested page the url will look like wwww.domain.com/username/page
I am having a few problems with passing on other data after page, I could set it up manually using lots of if statements but I would rather have a automatic way of passing on as many other elements as I like. Anyway here is the code (the part I am working on starts near the bottom line 102):
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Router Class
*
* Extends CI Router
*
* @author Original by EllisLab - extension by CleverIgniters
* @see http://codeigniter.com
*/
class MY_Router extends CI_Router {
/**
* Constructor
*
* @access public
*/
function MY_Router()
{
parent::CI_Router();
}
// --------------------------------------------------------------------
/**
* Validate Routing Request
*
* @access public
*/
function _validate_request($segments)
{
// Does the requested controller exist in the root folder?
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
{
return $segments;
}
// Is the controller in a sub-folder?
if (is_dir(APPPATH.'controllers/'.$segments[0]))
{
// Set the directory and remove it from the segment array
$this->set_directory($segments[0]);
$segments = array_slice($segments, 1);
if (count($segments) > 0)
{
// Does the requested controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
{
show_404($this->fetch_directory().$segments[0]);
}
}
else
{
$this->set_class($this->default_controller);
$this->set_method('index');
// Does the default controller exist in the sub-folder?
if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
{
$this->directory = '';
return array();
}
}
return $segments;
}
// Connect to server and select database.
mysql_connect("******", "*****", "*****")or die("cannot connect");
mysql_select_db("*****")or die("cannot select DB");
// Change select username from database
$query = "SELECT `username` FROM `members` WHERE `username` = '$segments[0]'";
$result = mysql_query($query);
// Count the number of results
$count = mysql_num_rows($result);
//Close mysql connection
mysql_close();
// If number of results = 0 the username doesnt exist
if($count==0){
// Redirect to nouser
if(isset($segments[0]) && sizeof($segments) == 1) {
return array('members', 'nouser', $segments[0]);
}
//If the username is taken redirect to the requested page
} else {
// Check the username segment is set
if(isset($segments[0])) {
//If there are 2 segments redirect to the chosen page
if(sizeof($segments) == 2){
return array('members', $segments[1], $segments[0]);
//If there is only one segment redirect to the user index page
} else {
return array('members', 'index', $segments[0]);
}
}
}
}
}
?>
You can see I have set it up so if there is more than one segment it will put the username first then the page second, but I cant figure out how to make it automatically add any more segments after the first 2 have been put in place. Any help would be much appreciated. Also my thanks to the authors of this script, it saved me lots of time!!