htaccess and POST variable issues with Codeigniter - php

So I am doing a bit of research on this topic, and you would think there would be an answer but there isn't, or maybe I am looking in the wrong area.
Problem
When submitting a form my post variables are empty. I am passing them through properly, the form goes where it needs to go to, and the variables and form data appear properly and with values, in the headers. I have the form helper autoloaded in the config, so everything is how it should be.
my form opener looks like this:
<form action='/Forms/form_processor/' method='post' id='testForm'>
In my Forms controller looks like this:
define('BASEPATH') OR exit('No direct script access allowed');
class Forms extends CI_Controller{
public function form_processor(){
// get variables
$name = $this->input->post('name');
// ... do stuff with variable data
}
}
I would also note that with the use of the htaccess file my config variables are:
$config['base_url'] = 'http://example.com/';
$config['index_page'] = ''; // this is to keep index.php out of the URL
Possible Cause
When using codeigniter and sitting at the home page, you get this URL:
http://example.com
That is fine, until you go to the next page, you get this URL:
http://example.com/index.php/page
It looks ugly, so for aesthetic purposes and to keep uniformity I created an htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /example.com/
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 http://example.com/
</IfModule>
and then by having this htaccess file at the base directory I get the URL to look like this:
http://example.com/page
Temp Fix and Questions Needing Answers
It was pointed out to me that because my $config['index_page'] variable is null that this is where the problems are stemming from. So I added the index.php to the beginning of my url in the form's action and it goes through.
Shouldn't the htaccess re-write handle this issue, and send it to where it is supposed to go where I have the index.php in the form's action URL or not?
Secondly, how would this affect the post variables? Because when I submit the form it still gets to where it needs to go to, it just shows null for all the variables, even though they are properly sent through the headers. This is where I am most confused.
If you need more information please let me know, but we are just trying to figure out why this is happening over here, it's kind of awkward. Thank you in advance!

Your question seems complicated. I think there might be an issue with installation steps. Remove index.php from config, set base url (also in application/config.php), set the encryption key(also there).
Check what are the session settings - did you change something there.
Also simplify htaccess (put it where is your index.php)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Is this local or server issue?
And last - try to use form helper, does it change something, whats inside console? Show more of your controller. You can check post inside one function
class Forms extends CI_Controller
{
function __construct()
{
parent::__construct();
}
function form_processor()
{
//check if $_POST
if ($this->input->post()) {
$name=$this->input->post('name');
//process and redirect
}
$this->load->view('forms/my_form');
}
}
Also if you use xss_filtering or csrf_protection(check that in config.php) you should definitely use form helper.

/forms/form_processor/' method='post' id='testForm'>
Use base URL in form action.
If not work so use index.php after base URL.

Related

Code Igniter Cannot access page via direct link

I am creating a project with Code Igniter as a back end framework and Bootstrap 3 as a front end framework.
I'm having an issue with accessing my pages via directly calling the controller followed by the method.
For example my controller is site.php and the method is home.
Here is what is looks like.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Site extends CI_controller {
public function index(){
$this->home();
}
public function home(){
$data["title"] ="SmartAgent";
$this->load->view("site_header");
$this->load->view("content_home", $data);
$this->load->view("site_footer");
}
As I understand the method index basically sets the method home as the index page.
When I type the web address in my url such as:
examplesite.co.uk
The controller correctly loads my view for the home method, which is content_home.php and the site loads the homepage along with the title fine.
However if I type:
examplesite.co.uk/site/home
This does not work! And I do not know why, this is further causing me issues such as URL's not working etc etc. However base url is set, and I can load CSS, JS, and image files fine. Also I have enabled helpers, routes and all else.
The above url works to load another project I was working on. So is why I know I'm missing something.
Any ideas anyone?
Thanks
Codeigniter routing is done relative to the index.php.
So your link should be examplesite.co.uk/index.php/site/home.
If that is the issue, then you need an .htaccess file, and in it write
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
(If I am not mistaken, writing from my phone)
Then, you will remove index.php from your site.
With codeigniter, you need to set $route[]. This is in application/config/routes.php
Also check out the codeigniter documentation on this, its pretty good and will explain it all.
Thank you everyone for your help and answers!
This was a silly mistake of mine and I wanted to let everyone know so that if anyone was to face this issue this may help. #Alexey answer above gave me a light bulb moment! So thank you.
Basically within the .htaccess mod rewrite file which can be downloaded from google. I forgot to change the directory for my server which is located at the top on line 4
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yourfolderdirectoryhere/
If you are unsure or unclear please watch below tutorial which helped me.
https://www.youtube.com/watch?v=dynPx1B0jis

Call PHP function with AJAX?

I know I can do this:
//Jquery
$.ajax({type: 'POST', data : {'action' : 'foo'}});
//PHP
if(isset($_POST['action']) && $_POST['action'] == 'foo')
{
function foo()
{
//yeah
}
}
But.. In a project I'm working on with a friend, he has set up the controllers to be able to have specific functions called using custom actions. For instance, say my view is using the controller of thing.php. From my Javascript I can just AJAX to a url like this:
'url' : 'thing'
So in this case, foo() would get called without a need for any ifs or switches (as far as I know)
To me, this is great and ideal but I'm not sure how it was set up. He isn't around for the holidays to ask so I'm asking you guys.
Do you know how this is achieved? We are using a pretty typical MVC architecture. Sorry I am a PHP novice. Thanks!
It looks like your friend is using .htaccess to rewrite URLS to add .php, perhaps with
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Now if you call a URL like /thing it will actually call the file /thing.php on your server and execute it. Or in your case if your url is just thing without the starting / it will call the thing.php in the same folder your current page is in.
Or perhaps he is rewriting everything to the controller and then adding the variable as command. Something like
RewriteRule ^(.*)$ controller.php?action=$1
Anyway, check your/his .htaccess file for clues

How to access a file via htaccess?

I have a folder structure like
Controller
|_check.php
View
|_ .htaccess
|_ index.php
|_ Webroot
|_ js
|_common.js
.htaccess
In .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ View/ [L]
RewriteRule (.*) View/$1 [L]
</IfModule>
In View/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
The outer .htaccess file links to View folder and there View/.htaccess links to index.php.
There is button in index.php(view file) with ajax to check.php.
I have given common.js The corresponding ajax function url as
var url = 'check.php'; // OR '../Controller/check.php'
The problem is the ajax is not working properly with two urls.
Whether I need change the folder structure to correct it or do I need to alter any htaccess file for accessing the check.php?
It's not about the folder structure but rather the htaccess rewrite rules that are wrong.
Your htaccess in the "root" redirects all requests to the View folder (which defaults to index.php, I assume)
I don't understand what you're trying to accomplish, if you explain I might be able to help you.
In your current setup, you can't access any file besides View/index.php (even when passing GET argument url)
EDIT:
In that case, if you wish to View/index.php be the only file accessible and force people to pass through View/index.php file, you can use PHP session variable.
Something like this...
in the top of your view.php file:
session_start();
$_SESSION['viewCheck'] = true;
//rest of view.php code
in your check.php code (or the file you're trying to access via AJAX)
session_start();
if (isset($_SESSION['viewCheck']) && $_SESSION['viewCheck'] === true) {
//Code of check.php
} else {
//Error message or redirect to view.php, for instance
//error message example
header('HTTP/1.0 401 Unauthorized');
//Redirect example
header("Location: http://www.yourhost.com/View/index.php");
}
NOTE 1:
You should remove the rewrite rules of your htaccess files.
NOTE 2:
Keep in mind that this is not bullet proof (and can be spoofed) since:
If someone visits View/index.php then he can access check.php freely. This can be mitigated if the session is killed after the ajax request. You can accomplish that if the ajax request consists in 2 requests, for instance, one to get a session key which expires in 10 seconds for instance, and then use that key to obtain the results from check.php
Session can be spoofed too (read more here)
Why dont you just set the path for the domain on the "View" folder, so noone can access you controllers etc. ?
I think this should also be more simple than playing around with .htaccess !
This is the way most PHP Frameworks do it..
And if you need to access the functions of check.php you can make a "ajax.php" that checks if the request is ok, and then uses "check.php" to catch the result!

Codeigniter URL Dilemma

I am having issues with the urls. I took the .htaccess code from the user guide to remove the index.php from the url and I have removed it from the config as well. Now here is the problem. I have a controller named “main” and a function inside called “join” which simply display the view. Now if I go to http://localhost/myfolder it loads the index view just fine. However if I try to go to http://localhost/myfolder/main/join or http://localhost/myfolder/join it gives me a 404 error. But http://localhost/myfolder/index.php/main/join works however without loading any css from my header file, but the footer is still loaded. Which I am very confused about. How can I possibly fix it so it just works with main/join? I will use routing later to make it just /join however I need the css to load like it does with my “index” view but not with my “join” view. Also localhost/myfolder/main gives me a 404 error as well. All I did was take the code from the user guide and paste it in .htaccess Any help guys?
Question is little confusing but let me try to answer what I got.
Question 1. Make 'localhost/myfolder/main/join' working.
You mainly need to remove index.php. Do the following:
a. (Very important) Make sure apache's rewrite module is enabled. IF you are using wamp, go to 'Apache'>'Apache modules' and make sure 'rewrite_module' is checked.
b. In code igniter, open file application>config>config.php. Search the line:
$config['index_page'] = 'index.php';
Remove index.php from there. Line must be
$config['index_page'] = '';
c. on myfolder, make a file .htaccess and add following code there:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Now URL 'localhost/myfolder/main/join' must be working.
Question 2: Load CSS
To load css, user following code under head section of your view:
<?php echo link_tag('style/style.css');?>
Make sure style/style.css is present in root folder; 'myfolder' in your case.
Hope this helps,
Kapil.

PHP Front Controller to get the Contents using HTACCESS file

I was trying to get make pretty URL. I have added a front end controller which gets the URL
and here is the Code for HTACCESS file and controller
RewriteEngine on
RewriteCond $1 !^(index\.php|search\.php|lib|css|ajax|includes|js|classes|parts|pages|images|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Controller file
$URL = $_SERVER["REQUEST_URI"];
$all_parts = explode("/", $URL);
My URL is something like
www.example.com/pretty-ulr/10.html
I get the page id which is page.php?p_id=10 (This is a simple example. I am using more complex example. )
But I am not sure how to get the contents here or redirect t0 page.php?p_id=10 to get show this page contents. When I redirect using header(), It does not get there
May be something small in .htaccess file, but I am not sure. Any idea please how to get the page contents.
I recommend using something like this in .htaccess:
RedirectMatch ^/$ /index.php
Then in php do:
$url=parse_url($_SERVER['REQUEST_URI']);
$urls=explode('/',rawurldecode(trim($url["path"],'/')));
new Controller($urls);
The class Controller would parse the urls and get the right view.

Categories