Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect with CodeIgniter

Can anyone tell me why my redirect helper does not work the way I'd expect it to? I'm trying to redirect to the index method of my main controller, but it takes me www.mysite.com/index/provider1/ when it should route to www.mysite.com/provider1. Does this make sense to anyone? I have index page in config set to blank, although I don't think that it is the issue. Does anyone have advice on how to fix this issue? Thanks in advance!

Controller:

if($provider == '') {     redirect('/index/provider1/', 'location'); } 

.htaccess:

RewriteEngine on  RewriteCond %{REQUEST_URI} !^(index\.php|files|images|js|css|robots\.txt|favicon\.ico)  RewriteCond %{HTTP_HOST} ^mysite.com/ttnf/ RewriteRule (.*) http://www.mysite.com/ttnf/$1 [R=301,L]  RewriteBase /ttnf/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]  php_flag display_errors On 
like image 734
ocergynohtna Avatar asked Apr 07 '09 01:04

ocergynohtna


People also ask

How to redirect same page in CodeIgniter?

You can also check $this->input->is_ajax_request() but it's just an idea. That's it, now you have a history of visited URLs. You can change whatever you want, and then back(1). Also useful for redirect your users to whatever they were doing before login.

Which function is used for page redirect in CodeIgniter?

While building internet software, we regularly need to redirect the consumer from one page to another web page. CodeIgniter makes this job clean for us. The redirect () function is used for this reason.

How do I link to another page in CodeIgniter?

site_url() : Returns your site URL, as specified in your config file. The index. php file (or whatever you have set as your site index_page in your config file) will be added to the URL, as will any URI segments you pass to the function, and the url_suffix as set in your config file.


2 Answers

redirect()

URL Helper


The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.

This statement resides in the URL helper which is loaded in the following way:

$this->load->helper('url'); 

The redirect function loads a local URI specified in the first parameter of the function call and built using the options specified in your config file.

The second parameter allows the developer to use different HTTP commands to perform the redirect "location" or "refresh".

According to the Code Igniter documentation: "Location is faster, but on Windows servers it can sometimes be a problem."

Example:

if ($user_logged_in === FALSE) {      redirect('/account/login', 'refresh'); } 
like image 147
Jon Winstanley Avatar answered Sep 23 '22 23:09

Jon Winstanley


If your directory structure is like this,

site   application          controller                 folder_1                    first_controller.php                    second_controller.php                 folder_2                    first_controller.php                    second_controller.php 

And when you are going to redirect it in same controller in which you are working then just write the following code.

 $this->load->helper('url');     if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic     {          redirect('same_controller/method', 'refresh');     } 

And if you want to redirect to another control then use the following code.

$this->load->helper('url'); if ($some_value === FALSE/TRUE) //You may give 0/1 as well,its up to your logic {      redirect('folder_name/any_controller_name/method', 'refresh'); } 
like image 40
Raham Avatar answered Sep 19 '22 23:09

Raham