Unable to get any $_POST data from any form - codeigniter

I have tried many google hits but I am yet unable to retrieve any POST data from forms, $_POST is just an empty array array(0) { }. I found few posts which was saying that problem is in .htaccess which doesn't correctly redirect my POST data but any of those posts didn't help me. I have tried many .htaccess files I found but none of them worked with POST data.
I am using Debian Jessie with Apache 2.4.10, PHP 5.6.14-0+deb8u1. I have AllowOverride all inside <Directory at my VHOST file. CodeIgniter 3.0.3. I have var_dump($_POST); at top of the index.php for testing. I also have enabled rewrite_module. There are no errors in the apache2 error.log file.
Here is my setup:
Controller: Admin.php
<?php
class Admin extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->library('session');
$this->load->library('form_validation');
}
public function index()
{
if(isset($this->session->userdata['logged']))
{
$this->load->view('admin/dashboard');
}
else
{
$this->load->view('templates/default/header');
$this->load->view('admin_login');
$this->load->view('templates/default/footer');
}
}
public function login()
{
$this->form_validation->set_rules('email', 'Email', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE)
{
echo "Validation failed!";
}
else
{
echo "Validation success!";
}
}
}
View: admin_login.php:
<form class="form-signin" method="post" action="/admin/login/">
<h2 class="form-signin-heading">Kirjaudu sisään</h2>
<label for="inputEmail" class="sr-only">Sähköpostiosoite</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<label for="inputPassword" class="sr-only">Salasana</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Salasana" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Muista minut
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Kirjaudu</button>
Current .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Your action is wrong. It should come like this
action="<?php echo base_url()?>admin/login/">
as well as load url in helpers
$this->load->helper('form','url');
as well as in these input tags there is no name attribute
<input type="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<input type="password" id="inputPassword" class="form-control" placeholder="Salasana" required>
Do like this
In controller
$this->load->helper('form'); #load library
In view
<?
$attributes = array('class' => 'form-signin');
echo form_open('admin/login/', $attributes);
$data1 = array(
'id' => 'inputEmail',
'class' => 'form-control',
'Placeholder' => 'Sähköpostiosoite',
'name' => 'email',
);
echo form_input($data1);
$data2 = array(
'id' => 'inputPassword',
'class' => 'form-control',
'Placeholder' => 'Salasana',
'name' => 'password',
);
echo form_password($data2);
echo form_submit('submit', 'Log In');
echo form_close();

Try this:
In controller add $this->load->helper(array('form','url')); in function __construct()
In View:
<form class="form-signin" method="post" action="<?php echo base_url()?>admin/login">
<h2 class="form-signin-heading">Kirjaudu sisään</h2>
<label for="inputEmail" class="sr-only">Sähköpostiosoite</label>
<input type="email" name="email" id="inputEmail" class="form-control" placeholder="Sähköpostiosoite" required autofocus>
<label for="inputPassword" class="sr-only">Salasana</label>
<input type="password" name = "password" id="inputPassword" class="form-control" placeholder="Salasana" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Muista minut
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Kirjaudu</button>
Hope this help! ^^

Related

POST method is not working in Codeigniter v4

I'm using form_open() for open form but form method is always showing get.
echo $this->request->getMethod() showing always get.
generated HTML is:
<form action="http://darkadmin.com/admin" method="post" accept-charset="utf-8">
<input type="hidden" name="_csrf" value="45435e3abd2d067883dacd6f62280fa7" />
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
</form>
View:
<?php if(isset($validation)) { ?>
<?php echo $validation->listErrors(); ?>
<?php } ?>
<?php echo form_open(current_url().'admin'); ?>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="Submit">
<?php echo form_close(); ?>
Controller:
<?php
namespace App\Controllers;
class Login extends BaseController
{
public function __construct(){
helper(['form', 'url']);
}
public function index()
{
$rules = [
'fname' => 'required',
'lname' => 'required'
];
echo $this->request->getMethod();
if($this->request->getMethod() == 'post' && $this->validate($rules)){
echo 'Success';
} else {
$this->data['validation'] = $this->validator;
}
return view($this->data['config']->theme.'login_view',$this->data);
}
}
How can I solve this problem? Please help me

in CodeIgniter form_validation not working

<form action="<?php echo base_url().'login/index'; ?>" method="post" name="loginForm" id="loginForm">
<div class="mb-3">
<input type="text" class="form-control" id="username" name="username" autocomplete="off">
</div>
<div class="mb-3">
<input type="password" class="form-control" id="password" name="password" autocomplete="off">
</div>
<button type="submit" class="btn btn-primary" name="submit">Submit</button>
</form>
Controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
public function index()
{
$this->load->library('form_validation');
$this->form_validation-
>set_rules('username','username','required');
$this->form_validation-
>set_rules('password','Password','required');
if($this->form_validation->run() == false){
$this->load->view('admin/login');
}
else{
echo"Form validation Successfully";
}
}
}
You are using base_url() without adding this library
// need this to use base_url() in the template.php
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
try with this line again

google recaptcha are not working?

controller: test.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test extends CI_Controller
{
function __construct()
{
parent :: __construct();
$this->load->helper(array('form', 'url'));
$this->load->model('Fetch_data');
}
public function login()
{
$this->load->view('login');
}
public function signin()
{
$firstName = trim($this->input->post('fname'));
$email = trim($this->input->post('email'));
$phone = trim($this->input->post('mobile'));
$recaptchaResponse = trim($this->input->post('g-recaptcha-response'));
$userIp=$this->input->ip_address();
$secret='******************************';
$url="https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response;=".$recaptchaResponse."&remoteip;=".$userIp;
$response = $this->curl->simple_get($url);
$status= json_decode($response, true);
if($status['success'])
{
$this->session->set_flashdata('flashSuccess', 'Google Recaptcha Successful');
}
else
{
$this->session->set_flashdata('flashSuccess', 'Sorry Google Recaptcha Unsuccessful!!');
}
redirect(base_url('index.php/test/login'));
}
}
view: login.php
<?php echo $this->session->flashdata('message');?>
<div class="error"><strong><?=$this->session->flashdata('flashSuccess')?></strong></div>
<hr/>
<form method="post">
<div class="form-group">
<input type="text" name="fname" id="fname" placeholder="Your First Name" class="text-line"/>
</div>
<div class="form-group">
<input type="text" name="email" id="email" placeholder="Your Email" class="text-line"/>
</div>
<div class="form-group">
<input type="text" name="mobile" id="mobile" placeholder="Your Mobile" class="text-line"/>
</div>
<div class="g-recaptcha" data-sitekey="*********************************"></div>
<div class="form-group">
<input type="submit" name="signup" id="signup" value="Sign Up" class="btn btn-warning"/>
</div>
</form>
In this code, I am using google recaptcha. when I have checked recaptcha checkbox
then it could not show message i.e. Google Recaptcha Successful or Sorry Google Recaptcha Unsuccessful. I don't know why ? can any body tell me where I am doing wrong ?

localhost is redirecting to 127.0.0.1 after submitting form in Codeigniter 3

I have a problem in my code. I have a form in my code that looks like this:
<?php echo form_open('users/auth/login', array('class' => 'form floating-label')); ?>
<div class="form-group">
<input type="text" class="form-control" id="username" name="username" />
<label for="username">Username</label>
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" name="password" />
<label for="password">Password</label>
<p class="help-block">Forgotten?</p>
</div>
<br/>
<div class="row">
<div class="col-xs-12 text-right">
<?php echo $this->session->flashdata('note'); ?>
<button class="btn btn-primary btn-raised btn-ink" type="submit">Login Account</button>
</div>
</div>
<?php echo form_close(); ?>
Then after clicking the submit it redirect my localhost to 127.0.0.1
After submitting the form it redirects me to
http://127.0.0.1/teradasys/index.php/user/login
Here's my controller
public function index() {
if($this->aauth->is_loggedin()) {
} else {
$data['page_header'] = 'Login Form';
$this->load->view('users/login', $data);
}
}
public function login() {
$identifier = $this->input->post('username');
$password = $this->input->post('password');
if ($this->aauth->login($identifier, $password, true)){
return true;
} else {
$note = $this->aauth->get_errors_array();
$this->session->set_flashdata('note', $note[0]);
$data['page_header'] = 'Login Form';
$this->load->view('users/login', $data);
}
}
Please check your base url in application/config/config.php
and change it.
$config['base_url'] = 'http://localhost/test/';
Please check your base url in application/config/config.php
change from
$config['base_url'] = 'http://localhost/test/';
to
$config['base_url'] = (isset($_SERVER['HTTPS']) ? "https://" : "http://") . $_SERVER['HTTP_HOST'] . preg_replace('#/+$#', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
$config['base_path'] = $_SERVER['DOCUMENT_ROOT'] . preg_replace('#/+$#', '', dirname($_SERVER['SCRIPT_NAME'])) . '/';
No need to change anything after placing this .If u r using local or live host.

404 error when redirection to a link codeigniter

I have hosted a site in my live server for testing.but i have used code to get to the specific controllers in my system .It is working well in my localhost but in my live server it doesnt work
Url are as follows
In local
a localhost:55/axxxx
b 69.1xx.xxx.xxx/~username/axxxx/axxxx
when calling a controller using a it works well but when calling the controller using b it gives me 404 error
here is my co`index.php/Authenticaton/authenticate">
<p>
<label for="login">Username</label>
<input type="text" name="username" placeholder="Username or email" required>
</p>
<p>
<label for="password">Password</label>
<input type="password" name='password' placeholder="Password" required>
</p>
<p>
<input type="submit" name="submit" id="login" class="btn-1" value="Continue">
</p>
</form>`
Pleas help me in this situation
Sorry for the Bad English
Thanks in Advance
Does it work while putting index.php before controller name, if so take a look at your htaccess.This might work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
might be base_url is pointing to different location.
Put this instead of base_url:
$root = "http://".$_SERVER['HTTP_HOST'];
$root .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$root";

Resources