PHP Method="Post" Stopped Working After I Added This .Htaccess... Why

PHP method=post stopped working after I added this .htaccess... Why?

This is happening because you are redirecting here:

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

When you redirect, the request BODY doesn't always get included, you can try adding an exception for POST:

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

form submit stops working after adding .htaccess file

Insert this rule after RewriteBase line to skip rewrite rules for POST requests:

RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]

After changing my .htaccess file, my POST method on my php no longer works

With external redirection POST data isn't redirected and is lost. Replace your .php rule by this:

# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /.+?\.php [NC]
RewriteRule ^(.+?)\.php$ /$1 [R=301,L,NE]

POST not working because of a not configured correctly htaccess file

I resolved my issue changing the model to :

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Login_model extends CI_Model {

function __construct()
{
parent::__construct();
}

function validate($username, $password){

$this->db->select('*');
$this->db->from('tbl_admin_users');
$this->db->where('username', $username);
$this->db->where('password', $password);

$query = $this->db->get();

return $query;
}
}

And the Controller function to :

public function login_validation(){

$username = $this->input->POST('username');
$password = $this->input->POST('password');

if($username != NULL && $password != NULL)
{
$validate = $this->Login_model->validate($username, $password);

$rows = $validate->num_rows();

if($rows > 0){

$data = $validate->row_array();

$nome = $data['nome'];
$apelido = $data['apelido'];
$id = $data['id'];
$username = $data['username'];
$nivel = $data['nivel'];

$sesdata = array(
'nome' => $nome,
'apelido' => $apelido,
'username' => $username,
'nivel' => $nivel,
'id' => $id,
'logged_in' => TRUE
);

$this->session->set_userdata($sesdata);

// access login for admin
if($nivel == '0'){
redirect(base_url() . 'Admin_Controller');

// access login for staff
}elseif($nivel == '1'){
redirect(base_url() . 'Admin_Controller/1');

// access login for author
}elseif($nivel == '2'){
redirect(base_url() . 'Admin_Controller/2');
}

}else{
echo $this->session->set_flashdata('msg','Username - '.$username.' or Password - '.$password.' is Wrong');
redirect(base_url().'Login_Controller');
}
}else
{
echo $this->session->set_flashdata('msg','Nenhum dos campos pode estar em branco !');
redirect(base_url().'Login_Controller');
}
}

also changed the .htaccess file to :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

# ALLOW ONLY NECESSARY REQUEST METHODS
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD|OPTIONS|POST|PROPFIND|PUT) [NC]
RewriteRule .* - [F,L]

htaccess and POST variable issues with Codeigniter

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.

not able to post the form because of .htaccess

I deleted the entire folder and uploaded the folder without .htaccess..The form worked...Later I uploaded the .htaccess (with the code that I posted in the question) and now working fine..

BUT FOR THE ANSWER AS TO WHY IT DID NOT WORK INITIALLY ,I DONT KNOW...FOR ANY ONE WHO KNOW CAN KINDLY POST..(CURIOUS TO KNOW)

Any way thanks for all those who helped me

Why are my forms not working after .htaccess URL rewrite?

Add this rule on top before other rules to skip all POST requests from rewrite:

RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^ - [L]

Seems like POST values are lost when .htaccess RewriteRule used. GET values are OK. How to fix?

The [R] flag will incur a redirect. And user-agents issue a redirect as GET request. There is nothing that can be done if you really want to shorten URLs down to the / root path.

You could however block POST requests specifically from being rewritten/redirected:

RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^index.php / [L,R=301]

Can't open class php in php file with htaccess

A simple rule for including files via a relative path where you have not configured an application level include_path; always base the relative path from that of the current script, eg

// public/login.php

require_once __DIR__ . '/../src/App/Login.php';

Because you're including public/login.php from index.php, the include path includes the parent directory of index.php, ie mysite. This applies across any files included.

When public/login.php tries to include ../src/App/Login.php it is actually attempting to open htdocs/mysite/../src/App/Login.php.

Another thing you might want to try is configure an application level include path. For example, in index.php...

set_include_path(implode(PATH_SEPARATOR, [
__DIR__ . '/src/App',
get_include_path()
]));

Now your src/App directory is the first searched when performing an include or require so you can simply run

require_once 'Login.php';

Update

An even better solution would be to register an autoloader, for example (in index.php)

spl_autoload_register(function($class) {
require_once sprintf('%s/src/App/%s.php', __DIR__, $class);
});

Then you can simple create your class without worrying about including the file...

$login = new Login();


Related Topics



Leave a reply



Submit