CodeIgniter Forums
Problem with Form Validation and Htaccess file - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Problem with Form Validation and Htaccess file (/thread-52026.html)

Pages: 1 2 3 4


Problem with Form Validation and Htaccess file - El Forum - 05-27-2012

[eluser]DavidHopkins[/eluser]
Hello all, I am trying to make a simple log in feature that works fine when i dont make use of a htaccess file in order to remove the index.php part of a url, but when i go into my config and remove the index.php from the following line my form validation always returns false.
Code:
$config['index_page'] = 'index.php';

My code looks like this

Code:
public function ProcessLogin() {
  
  $this->load->library('form_validation');
  $this->load->helper('form');
  $this->load->helper('url');
  $this->form_validation->set_rules('cid', 'a', 'trim|required|xxs_clean');
  $this->form_validation->set_rules('email', 'b', 'trim|required|valid_email|xxs_clean');
  $this->form_validation->set_rules('pass', 'c', 'trim|required|xxs_clean');
  
  extract($_POST);
  
  if($this->form_validation->run()==False) {
   echo"false";
  }
  else {
  echo"ok";
  }
}


Code:
<div id="LoginContainer">
&lt;?=form_open('login/ProcessLogin');?&gt;
<h1>Login</h1>
    
    <div id="LoginContainerRow">
     <div id="LoginContainerLabel">Account No:</div>
        <div id="LoginContainerInput">&lt;input type="text" name="cid" /&gt;&lt;/div>
    </div>
    
    <div id="LoginContainerRow">
     <div id="LoginContainerLabel">Email Address:</div>
        <div id="LoginContainerInput">&lt;input type="text" name="email" /&gt;&lt;/div>
    </div>
    
    <div id="LoginContainerRow">
     <div id="LoginContainerLabel">Password:</div>
        <div id="LoginContainerInput">&lt;input type="password" name="pass"  /&gt;&lt;/div>
    </div>
    
    <div id="LoginContainerButton">
     &lt;input name="" type="submit" value="Log In"/&gt;
    </div>
&lt;/form&gt;
</div>

this is my htaccess file

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocum

Im not sure why it dosent work when trying to remove the index.php

any help would be great !

Dave


Problem with Form Validation and Htaccess file - El Forum - 05-27-2012

[eluser]InsiteFX[/eluser]
You do not need these anymore, they were for the old versions of CodeIgniter!
Code:
#Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]
    
    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

CodeIgniter now has .htaccess files in all the directories to protect them.

I would also autoload the url_help in ./application/config/autoload.php
Being as it is almost always used through out the application.

Also you are not giving a name to your submit input!



Problem with Form Validation and Htaccess file - El Forum - 05-27-2012

[eluser]Aken[/eluser]
Probably unrelated, but you spelled "xss_clean" wrong in your rules. Also, put the actual rules before the sanitize functions (IE: "required|trim|xss_clean").

.htaccess shouldn't affect the way your form_validation works, unless your .htaccess file is manipulating post data or URL redirects or something of that nature. Get everything working without .htaccess first, then go from there.


Problem with Form Validation and Htaccess file - El Forum - 05-28-2012

[eluser]DavidHopkins[/eluser]
Thanks for your replies all.

I have managed to get this working without the use of a htaccess file, however as soon as i change
Code:
$config['index_page'] = 'index.php';
to
Code:
$config['index_page'] = '';
in my config/config.php file i start to get errors.

FOr some reason i get errors relating to no such variables cid, email and pass which are the names of my form elements

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: email

Filename: controllers/login.php

Line Number: 28

Thats one error, but i dont get that when i have index.php in my config file. Its really odd and starting to bother me a little bit

Here is my htaccess file

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

Thanks

Dave


Problem with Form Validation and Htaccess file - El Forum - 05-28-2012

[eluser]InsiteFX[/eluser]
Dave, what os are you running?



Problem with Form Validation and Htaccess file - El Forum - 05-28-2012

[eluser]DavidHopkins[/eluser]
Linux, Ubuntu

Edit,

I know htaccess files are working because i haev a wordpress install using a htacess file for clean links also


Problem with Form Validation and Htaccess file - El Forum - 05-28-2012

[eluser]InsiteFX[/eluser]
No, that's fine I was only asking because on windows it doe's not use the .c it uses .so

Did you try using the full php tags on the form_open?
Code:
&lt;?php echo form_open('login/ProcessLogin'); ?&gt;

This is strange because I run Windows 7 Pro and I can remove index.php and it works fine with forms.



Problem with Form Validation and Htaccess file - El Forum - 05-28-2012

[eluser]DavidHopkins[/eluser]
Yep just tried that and its the same result.

Its really odd im not sure whys its doing it because i copied the code from a different project i had been working on.

=[

Edit,

itd odd because i can navigate to /login/testfunction which just echos a string just fine.

It just seems that for some reason the post is losing its variables.


Problem with Form Validation and Htaccess file - El Forum - 05-28-2012

[eluser]InsiteFX[/eluser]
Try this
Code:
public function ProcessLogin() {
  
  $this->load->library('form_validation');
  $this->load->helper('form');
  $this->load->helper('url');
  $this->form_validation->set_rules('cid', 'a', 'trim|required|xxs_clean');
  $this->form_validation->set_rules('email', 'b', 'trim|required|valid_email|xxs_clean');
  $this->form_validation->set_rules('pass', 'c', 'trim|required|xxs_clean');
  
  extract($_POST);

  // see if you are getting the post data
  var_dump($_POST);
  exit;



  if($this->form_validation->run()==False) {
   echo"false";
  }
  else {
  echo"ok";
  }
}



Problem with Form Validation and Htaccess file - El Forum - 05-28-2012

[eluser]DavidHopkins[/eluser]
Nope comes back

array(0) { }

Reading on the Internet alot of people seem to have troubles with post data and the htaccess file but no one seems to have provided an answer.

I know it can be done thou, because i have had it working on a different project some time ago now.

Dave