How to do through routing in Codeigniter? - codeigniter

Now I have some rules for routing in file CI routes.php:
$route['^fr/(.+)$'] = "$1";
$route['^en/(.+)$'] = "$1";
It works when I use URL as:
http://test.com/en/news
But does not work for:
http://test.com/en/news/3
Or
http://test.com/en/news/4/1/3/any
After there are a routing for news:
//NEWS
$route['news/search'] = "news/search";
$route['news/(:num)'] = "news/getNew/$1";
$route['news/category/(:num)/(:any)'] = "news/filter/$1";
$route['news/category/(:num)'] = "news/filter/$1";

Related

Codeigniter and Ckfinder csrf_exclude_uris

I'm having an issue with Codeigniter 3 and CKfinder regards the CSRF Protection
If I use the below in my Codeigniter Config file CKFinder image upload works fine
$config['csrf_protection'] = FALSE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();
if I change the $config['csrf_protection'] = TRUE; CKFinder image uploads fail
What I need is to be able to exclude CKFinder from falling under the CSFR Protection - I've tried the below but nothing seems to work:
$config['csrf_exclude_uris'] = array('assets/plugins/ckfinder/.*+', 'assets/plugins/ckfinder/ckfinder.js', 'assets/plugins/ckfinder', 'admin/news/.*+');
Any pointers would be appreciated
Hope this may work. it worked in my case.
$config['csrf_exclude_uris'] = array('assets/plugins/ckfinder/[\s\S]*');
but as far my knowledge "assets" resources don't need any csrf protection.
# Its work fine #
$config['csrf_protection'] = TRUE;
if(isset($_SERVER["PHP_SELF"])){
$parts = explode("/",$_SERVER["PHP_SELF"]);
$exclude_url_arr = array('login');
if (!empty($exclude_url_arr[0])) {
foreach($parts as $part) {
if (in_array($part,$exclude_url_arr)) {
$config['csrf_protection'] = FALSE;
break;
}
}
}
}
URL which submit the form data.
Add lines below in config.php file. This will work.
$config['**csrf_exclude_uris**'] = array(
*'device/deviceProcess/pushData'*
);
-- deviceProcess : Controller
--- pushData : Method
----- URL : http://hostname/index.php/device/deviceProcess/pushData

Inconsistent CodeIgniter routes.php behaviour

I'm using CodeIgniter's routes file to remap short urls to their relevant controller functions.
Oddly enough, some urls get remapped, while others don't, even though the syntax is identical.
Below is my routes.php file. The remapping of info/web and info/rent works as expected, while info/cell and info/hotel gets redirected to my 404 function.
Any idea what could be causing it?
$route['default_controller'] = "home";
$route['404_override'] = 'home/four_o_four';
$route['robots.txt'] = 'home/robots';
$route['info/cell'] = "articles/tags/cellphone";
$route['info/rent'] = "articles/tags/car-rental";
$route['info/web'] = "articles/tags/internet-abroad";
$route['info/hotel'] = "articles/tags/hotel";
$route['articles/tags/(:any)'] = "articles/articles_by_tags/$1";
$route['articles/destination/(:any)'] = "articles/articles_by_destination/$1";
$route['articles/(:any)'] = "articles/show_article/$1";
You are re-routing one URI to another re-routed URI.
$route['info/cell'] = "articles/tags/cellphone";
$route['info/rent'] = "articles/tags/car-rental";
$route['info/web'] = "articles/tags/internet-abroad";
$route['info/hotel'] = "articles/tags/hotel";
$route['articles/tags/(:any)'] = "articles/articles_by_tags/$1";
You are supposed to route from a URI to a Controller/Method, not to another URI.
Just route them all directly to each corresponding Controller/Method...
$route['info/cell'] = "articles/articles_by_tags/cellphone";
$route['info/rent'] = "articles/articles_by_tags/car-rental";
$route['info/web'] = "articles/articles_by_tags/internet-abroad";
$route['info/hotel'] = "articles/articles_by_tags/hotel";
$route['articles/tags/(:any)'] = "articles/articles_by_tags/$1";
Routes Documentation

Codeigniter Internationalization i18n trailing slash not added

I have a project going on, and now [admin section is almost done] (I know bit late) I am trying to implement i18n to the project.
I think everything works fine except when I enter http://localhost/my_project while my_project is working directory with CI installation in it, I am redirected to the following http://localhost/my_project/enhome (no trailing slash after en) any ideas?
Expecting result http://localhost/my_project/en/home, not just home controller but all controllers are behaving same.
.htaccess, base_url and index_page are set right (all works without the i18n).
routes.php are out of stock
$route['default_controller'] = "home";
$route['404_override'] = '';
// URI like '/en/about' -> use controller 'about'
$route['^(en|de|fr|nl)/(.+)$'] = "$2";
// '/en', '/de', '/fr' and '/nl' URIs -> use default controller
$route['^(en|de|fr|nl)$'] = $route['default_controller'];
edit
I am using "new" i18n from I guess ellislab. Sorry for wrong link (for 1.7 CI version).
So after some digging, I found the problem in core file, on the bottom of this posted script there is variable $new_url and it was echoing Redirect triggered to http://192.168.1.184/my_project/enhome without the "fix". I just added the / in there and it works prefectly I wonder what happend here and if this is OK fix or not.
function MY_Lang() {
parent::__construct();
global $CFG;
global $URI;
global $RTR;
$this->uri = $URI->uri_string();
$this->default_uri = $RTR->default_controller;
$uri_segment = $this->get_uri_lang($this->uri);
$this->lang_code = $uri_segment['lang'] ;
$url_ok = false;
if ((!empty($this->lang_code)) && (array_key_exists($this->lang_code, $this->languages)))
{
$language = $this->languages[$this->lang_code];
$CFG->set_item('language', $language);
$url_ok = true;
}
if ((!$url_ok) && (!$this->is_special($uri_segment['parts'][0]))) // special URI -> no redirect
{
// set default language
$CFG->set_item('language', $this->languages[$this->default_lang()]);
$uri = (!empty($this->uri)) ? $this->uri: $this->default_uri;
//OPB - modification to use i18n also without changing the .htaccess (without pretty url)
$index_url = empty($CFG->config['index_page']) ? '' : $CFG->config['index_page']."/";
$new_url = $CFG->config['base_url'].$index_url.$this->default_lang()."/".$uri;
echo "Redirect triggered to ".$new_url;
//header("Location: " . $new_url, TRUE, 302);
// exit;
}
}
this library is little old never been updated author since long time and it works for CI 1.7 but here is updated version compatible with CI 2.1.* same library but updated version working same way you can download from here
CodeIgniter 2.1 internationalization i18n

Unable to Connect - http://10.0.2.2/test.php Local Host XAMPP

I have made a program using PHP Script, but whenever i am trying to run my script using Browser getting Problem Loading Page:Firefox can't establish a connection to the server at 10.0.2.2.
I have saved test.php under - E:\xampp\htdocs
test.php:
<?php
$objConnect = mysql_connect("localhost","root","");
$objDB = mysql_select_db("registration_login");
$_POST["sUsername"] = "testname";
$_POST["sPassword"] = "testpassword";
$_POST["sName"] = "testname";
$_POST["sEmail"] = "testemail";
$_POST["sTel"] = "testtel";
$strUsername = $_POST["sUsername"];
$strPassword = $_POST["sPassword"];
$strName = $_POST["sName"];
$strEmail = $_POST["sEmail"];
$strTel = $_POST["sTel"];
/*** Check Username Exists ***/
$strSQL = "SELECT * FROM member WHERE Username = '".$strUsername."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
$arr['StatusID'] = "0";
$arr['Error'] = "Username Exists!";
echo json_encode($arr);
exit();
}
/*** Check Email Exists ***/
$strSQL = "SELECT * FROM member WHERE Email = '".$strEmail."' ";
$objQuery = mysql_query($strSQL);
$objResult = mysql_fetch_array($objQuery);
if($objResult)
{
$arr['StatusID'] = "0";
$arr['Error'] = "Email Exists!";
echo json_encode($arr);
exit();
}
/*** Insert ***/
$strSQL = "INSERT INTO member (Username,Password,Name,Email,Tel)
VALUES (
'".$strUsername."',
'".$strPassword."',
'".$strName."',
'".$strEmail."',
'".$strTel."'
)
";
$objQuery = mysql_query($strSQL);
if(!$objQuery)
{
$arr['StatusID'] = "0";
$arr['Error'] = "Cannot save data!";
}
else
{
$arr['StatusID'] = "1";
$arr['Error'] = "";
}
mysql_close($objConnect);
echo json_encode($arr);
?>
I recommend you to use your machine's IP Address in place of 10.0.2.2
So complete URL should look like this:
http://ipaddress/test.php
Note: Above solution for while you want to use Local Server via Android Emulator
Do you know where the 10.0.2.2 is coming from? By default localhost resolves to 127.0.0.1. This could potentially be your problem.
The problem you are experiencing wouldn't be from your PHP script. But rather the request isn't getting from your browser to your Apache web server.
- You'll want to ensure Apache is running.
- You could try making sure you are using localhost in the URL.
- You could check the apache log files under /Xampp/apache/logs/ and see if there is any recent errors being logged.
- You could also try a tool like Fiddler to see the exact response of the request.
Hope that helps.

CodeIgniter, rewrite urls, call custom controller

I have completed CMS.
This CMS have many controllers: news, content, articles, etc.
Also have modules like "modules/shop/controllers/cart.php", "modules/shop/controllers/items.php".
ATM URLs are:
/shop/some-category/
/shop/nokia/n73.html
/content/about-company.html
/articles/phones/full/1.html
Now I need urls like:
- /nokia.html
- /my/super/article-about-phones.html
So, I need custom URLs for any controlles (shop, news, content, etc).
As I know, I can't execute controller from controller.
How I can solve a problem?
I have no idea how to rewrite Router core class. Any ideas?
For me I have 1 way - rewrite architecture of full product but I have no time :(
Thnx alot.
P.S. ATR routes.php is:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
$lang_prefix = '(([a-z]{2}/)?)';
$route[$lang_prefix.'ajax/shop/(.*)'] = "shop/ajax/$3";
$route['cron/(.*)'] = "cron/$1";
$route[$lang_prefix.'content/(.*)'] = "content/index";
$route[$lang_prefix.'faq/ask/(.*)'] = "faq/ask";
$route[$lang_prefix.'faq/(.*)'] = "faq/index";
$route[$lang_prefix.'sitemap'] = "sitemap/index";
$route[$lang_prefix.'contact'] = "contacts/index";
$route[$lang_prefix.'news/(.*?)/full/[0-9]+'] = "news/full";
$route[$lang_prefix.'news/(.*)'] = "news/index";
$route[$lang_prefix.'compare/[0-9]+[0-9\/]+'] = "shop/compare";
$route[$lang_prefix.'cart/(.*?)'] = "shop/cart/$3";
$route[$lang_prefix.'cart'] = "shop/cart";
$route[$lang_prefix.'order'] = "shop/order";
$route[$lang_prefix.'order/check'] = "shop/order";
$route[$lang_prefix.'order/successful/[A-z_\-0-9]+'] = "shop/order/successful";
$route[$lang_prefix.'search/(name|price|rating)/(asc|desc)(.*)'] = "shop/search";
$route[$lang_prefix.'shop/(.*)/(name|price|rating)/(asc|desc)(.*?)'] = "shop/items/index";
$route[$lang_prefix.'shop/(.*?)/(.*?)/add-comment'] = "shop/items/add_comment";
$route[$lang_prefix.'shop/(.*?)/(.*?)'] = "shop/items/details";
$route[$lang_prefix.'shop/(.*)'] = "shop/items/index";
$route[$lang_prefix.'feedback'] = "feedback/index";
$route[$lang_prefix.'callback'] = "feedback/index";
$route[$lang_prefix.'article/(.*?)/full/(.*)'] = "articles/full";
$route[$lang_prefix.'article/(.*)'] = "articles/index";
$route[$lang_prefix.'gallery/(.*?)/full/(.*)'] = "gallery/full";
$route[$lang_prefix.'gallery/(.*)'] = "gallery/index";
$route[$lang_prefix.'admin/ajax/(.*)'] = "admin/ajax/$3";
$route[$lang_prefix.'admin'] = "admin/main/index";
$route[$lang_prefix.'admin/login'] = "admin/auth/login";
$route[$lang_prefix.'admin/logout'] = "admin/auth/logout";
$route[$lang_prefix.'admin/(.*)'] = "admin/$3";
$route['default_controller'] = "shop/showcase";
You could use a mod_rewrite to direct all requests to a .html file to go to a special static content controller. So:
my/super/article-about-phones.html
would redirect to:
/content/static/article-about-phones/
This might look something like this:
Options +FollowSymLinks
RewriteBase /
RewriteEngine On
RewriteRule ^(.+)/([^/]+)\.html$ /content/static/$1/ [NC,L]

Resources