I've spent most of today trying to work out why my CodeIgniter application returns empty $_POST (and $this->input->post(<var>) and php://input) - I'd reduced the application down to a utterly minimal example of a single form input element and echoing $_POST.
As requested, the files (but, as far as I can see, this is irrelevant to my question of why the lack of the .htaccess rules prevents POST data being submitted through the framework):
The controller was this:
<?php
class Form extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('form');
$this->output->enable_profiler(TRUE);
}
public function form_show() {
$this->load->view("form");
}
public function data_submitted() {
$data['user_name'] = $this->input->post('u_name');
$this->load->view("form-success", $data);
}
}
?>
form(.php) was:
<html>
<head></head>
<body>
<?php
echo form_open('Form/data_submitted');
echo form_label('User Name :', 'u_name');
$form_data = array('name' => 'u_name');
echo form_input($form_data);
$submit_data = array(
'type' => 'submit',
'value'=> 'Submit',
);
echo form_submit($submit_data); ?>
echo form_close();
if(isset($user_name)){
echo "<p>".$user_name."</p>";
} ?>
</body>
</html>
form-success(.php) was
<?php var_dump($this->_ci_cached_vars); ?>
<html>
<head></head>
<body>
<?php print_r($_POST); ?>
</body>
</html>
I then realised I didn't have a .htaccess in my root folder - so restored CodeIgniter's default .htaccess file of
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
But I can't work out which one of these (or why) allows POSTed data to be passed to the controller, but removing these rules doesn't allow it. Any light anyone can shed on this would be hugely appreciated to help satisfy my curiosity!
Related
After searching for hours I still don't get why I get a 404 error when I do a form_submit.
My controller looks like this:
class pages extends CI_Controller{
function view($page = 'home'){
$this->load->helper('url');
if(!file_exists('application/views/pages/'.$page.'.php')){
show_404();
}
$data['title'] = $page;
$this->load->view('templates/view',$data);
}
public function login_validation(){
}
The view page:
<?php echo form_open('pages/login_validation');
echo form_input(array(
'name' => 'firstname',
'placeholder' => 'Voornaam',
'class' => 'form-control input-lg',
'tapindex' => '1'
));
echo form_submit(array(
'name' => 'login_submit',
'value' => 'Register',
'class' => 'btn btn-primary'
));?>
.htaccess (in the root folder):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|indexcp\.php?|resources|robots\.txt)
RewriteRule ^([A-Za-z0-9_/.-]+)$ index.php?/$1
I have this line in the autoload.php:
$autoload['helper'] = array('form','url');
In config.php:
$config['base_url'] = 'http://localhost/';
$config['index_page'] = '';
routes.php:
$route['(:any)'] = "pages/view/$1";
$route['default_controller'] = "pages/view";
$route['404_override'] = '';
What is wrong with this configuration?
The problem you are having is with the following route (I am assuming you've posted the whole of your routes file):
$route['(:any)'] = "pages/view/$1";
This means that any file you load is going to go to the pages controller, call the function view, and pass in the parameter which is as $1. In this case, it's going to pass through either 'pages/login_validation' or 'login_validation' (I'm not 100% sure which, and I can't test at the moment.
Your view function is checking to see if the file 'application/view/pages/login_validation.php' or 'application/view/pages/pages/login_validation.php' exists, and when it can't find it, it is giving the 404 page.
To fix this, you can add the following into the routes.php file (above the $route[(:any)] = 'pages/view/$1'; line):
$route['pages/login_validation'] = 'pages/login_validation';
That will explicitly check that the page wanting to be loaded is the login_validation one, and then call that function within the pages controller.
If you don't want to keep adding routes each time, then you could modify the view function in pages to check if the $page variable matches the name of a function, and if so then call that function:
if (function_exists($page))
{
$page();
}
The trouble you might have then is that you won't easily be able to pass through additional parameters to the given page. You might also accidentally name a page the same as a function in PHP, and then it will try and call that instead of loading the page you intend.
I have a login system that works, but on the redirect function it gives me the error The requested URL "http://localhost/musiclear/index.php/welcome" cannot be found or is not available. Please check the spelling or try again later.
This is where I am using it (login.php):
function validate_credentials() {
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if ($query) { // if users credentials validated
$data = array('usernames' => $this->input->post('username'),
'is_logged_in' => true);
$this->session->set_userdata($data); //set session data
redirect('welcome', 'refresh'); //redirect to home page
} else { //incorrect username or password
$this->index();
}
}
This is where I am directing it to (welcome.php):
class Welcome extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title'; // $title
$data['page_header'] = 'Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
// just stored the array of objects into $data['firstnames] it will be accessible in the views as $firstnames
$data['users'] = $this->model_users->getUsers();
$this->load->view('home_view', $data);
}
}
Im thinking it is something wrong with the path, or where its linking to but im not sure. This is my directory setup:
Can someone please tell me whats wrong and how I can make it link to my page? Thanks so much
Have you created the .htaccess in your application folder?
Maybe this can work for your project:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /musiclear/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /musiclear/index.php
</IfModule>
You are welcome
I am stuck and cannot seem to figure my issue out. I am trying to write a basic "Contact Us Form" using Codeigniter for the first time. My form on my view looks like this..
<div style='float: left; padding-right: 55px; padding-left: 35px;'>
<?php
$this->load->helper('form');
echo form_open('pages/emailsender'); ?>
Contact Us:<br />
<?php
echo form_input('name', 'Enter Your Name');
echo form_input('email', 'Enter Your Email');
echo "<br />";
echo form_textarea('message', 'Enter your message here, thanks for taking the time to contact us!');
echo "<br />";
echo form_submit('submit', 'Send Message!');
echo form_reset('reset', 'Reset Form');
echo form_close();
?>
</div>
and my controller pages.php looks like this..
<?php
class Pages extends CI_Controller {
public function index(){
$this->load->view('templates/header');
$this->load->view('home');
$this->load->view('templates/footer');
}
public function home(){
$this->load->view('templates/header');
$this->load->view('home');
$this->load->view('templates/footer');
}
public function about(){
$this->load->view('templates/header');
$this->load->view('about');
$this->load->view('templates/footer');
}
public function services(){
$this->load->view('templates/header');
$this->load->view('services');
$this->load->view('templates/footer');
}
public function reviews(){
$this->load->view('templates/header');
$this->load->view('reviews');
$this->load->view('templates/footer');
}
public function specials(){
$this->load->view('templates/header');
$this->load->view('specials');
$this->load->view('templates/footer');
}
public function contact(){
$this->load->view('templates/header');
$this->load->view('contact');
$this->load->view('templates/footer');
}
public function emailsender(){
$this->load->view('templates/header');
$this->load->view('contact');
$this->load->view('templates/footer');
}
}
I have the "emailsender" function just redirecting me back to the same page as a test, but it won't even do that?! I get a "No input file specified." error every time. I read this post on stackoverflow Codeigniter no input file specified error but it doesn't seem to resolve my issue. My config file looks like this..
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
| If this is not set then CodeIgniter will guess the protocol, domain and
| path to your installation.
|
*/
$config['base_url'] = 'http://mywebsitename.com';
$config['index_page'] = "";
$config['uri_protocol'] = "AUTO";
any ideas why I am getting the No input file specified error?
Change your .htaccess to look like this
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Notice the ? mark in the last line. Previously this line was something like this RewriteRule ^(.*)$ index.php/$1 [L]
Also make sure to add this line RewriteBase / in your .htaccess
Change Your .htaccess like that
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
Hope It will works for you...
thanks
Change:
echo form_open('pages/emailsender'); ?>
To:
echo form_open_multipart('pages/emailsender'); ?>
form_open_multipart adds the enctype="multipart/form-data" to the form tag.
You need to use the direct link instead of the short hand of the form open tag. Like this:
echo form_open(‘http://example.com/welcome/emailsender’);
instead of this:
echo form_open(‘welcome/emailsender’);
I wonder if anyone can help me??? I'm using CodeIgniter to create a small Login Screen. However, every time I call the function to validate the details using the CodeIgniter form helper. A 404 error keeps showing stating that CodeIgniter can't find the URL. My code for the 'View' file looks like:
<!doctype html>
<html>
<head>
<title>CodeIgniter Prototype</title>
<!-- CSS/LESS Code Links -->
<link rel = "stylesheet" href = "<?php echo base_url();?>css/style.css" type = "text/css"/>
</head>
<body>
<div class = "login_wrapper">
<h1>ONESG OneInvoice Exporter</h1>
<?php
echo form_open('login/check_login');
echo form_input('username', '', 'placeholder = "Enter Username"');
echo form_password('password', '', 'placeholder = "Enter Password"');
echo form_submit('submit', 'Login');
echo form_close();
?>
</div>
</body>
</html>
My Controller Code Looks Like:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_Controller {
public function index() {
$this->load->view('login_form');
}
public function check_login() {
$this->load->model('login_model');
$query = $this->login_model->validate();
if ($query) {
$data = array (
'username' => $this->input->post('username'),
'is_logged_in' => true
);
$this->sessions->set_userdata($data);
redirect('site/members_area');
}
else {
$this->index();
}
}
}
Check your folder structure whether you have .htaccess file.
if not add file name called .htaccess
and add following code to file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Add this in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Then in routes.php in config add
$route["members_area/(.*)"] = 'site/members_area/$1';
How can i create a main page with codeigniter?
That page should contain a few links like login, register, etc.
I followed a tut to create a login screen. But it made codeigniter only for that purpose. This is the site i'm talking about:
http://tutsmore.com/programming/php/10-minutes-with-codeigniter-creating-login-form/
So basically what im trying to is, use codeigniter for more things than just a login form.
My try routes.php i set these settings:
$route['default_controller'] = "mainpage";
$route['login'] = "login";
My mainpage.php file:
class Mainpage extends Controller
{
function Welcome()
{
parent::Controller();
}
function index()
{
$this->load->view('mainpage.html');
}
}
Mainpage.html:
<HTML>
<HEAD>
<TITLE></TITLE>
<style>
a.1{text-decoration:none}
a.2{text-decoration:underline}
</style>
</HEAD>
<BODY>
<a class="2" href="login.php">login</a>
</BODY>
</HTML>
Login.php looks exactly like the one in that website which i provided the link for in this post:
Class Login extends Controller
{
function Login()
{
parent::Controller();
}
function Index()
{
$this->load->view('login_form');
}
function verify()
{
if($this->input->post('username'))
{ //checks whether the form has been submited
$this->load->library('form_validation');//Loads the form_validation library class
$rules = array(
array('field'=>'username','label'=>'username','rules'=>'required'),
array('field'=>'password','label'=>'password','rules'=>'required')
);//validation rules
$this->form_validation->set_rules($rules);//Setting the validation rules inside the validation function
if($this->form_validation->run() == FALSE)
{ //Checks whether the form is properly sent
$this->load->view('login_form'); //If validation fails load the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> form again
}
else
{
$result = $this->common->login($this->input->post('username'),$this->input->post('password')); //If validation success then call the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> function inside the common model and pass the arguments
if($result)
{ //if <b style="color: black; background-color: rgb(153, 255, 153);">login</b> success
foreach($result as $row)
{
$this->session->set_userdata(array('logged_in'=>true,'id'=>$row->id,'username'=>$row->username)); //set the data into the session
}
$this->load->view('success'); //Load the success page
}
else
{ // If validation fails.
$data = array();
$data['error'] = 'Incorrect Username/Password'; //<b style="color: black; background-color: rgb(160, 255, 255);">create</b> the error string
$this->load->view('login_form', $data); //Load the <b style="color: black; background-color: rgb(153, 255, 153);">login</b> page and pass the error message
}
}
}
else
{
$this->load->view('login_form');
}
}
}
What am i missing guys?
You're using CodeIgniter, right? Do you have some stipulation in your config that you must use .php as an extension on all URLs? If not, you should be sending your hrefs to "/login" not "/login.php". Furthermore, if you haven't removed the "index.php" from your URL in your htaccess file and CI config, you'll probably need to include that in your links.
Furthermore, if I were you, I'd not follow what Sarfraz does in his Mainpage.php file. You shouldn't be using standard PHP includes in CodeIgniter. Anything that is done with an include can easily be done by loading a view. For example, if you want to load the view as a string, you can say:
$loginViewString = $this->load->view('login.php', '', true);
Where the second parameter is whatever information you want passed to your view in an associative array where the key is the name of the variable you'll be passing and the value is the value. That is...
$dataToPassToView = array('test'=>'value');
$loginViewString = $this->load->view('login.php', $dataToPassToView, true);
And then in your login.php view you can just reference the variable $test which will have a value of "value".
Also, you don't really need to have that "login" route declared since you're just redirecting it to the "login" controller. What you could do is have a "user" controller with a "login" method and declare your route like this:
$routes['login'] = 'user/login';
EDIT...
OK, I think this has perhaps strayed one or two steps too far in the wrong direction. Let's start over, shall we?
First let's start with a list of the files that are relevant to this discussion:
application/controllers/main.php (this will be your "default" controller)
application/controllers/user.php (this will be the controller that handles user-related requests)
application/views/header.php (I usually like to keep my headers and footers as separate views, but this is not necessary...you could simply echo the content as a string into a "mainpage" view as you're doing....though I should mention that in your example it appears you're forgetting to echo it into the body)
application/views/footer.php
application/views/splashpage.php (this is the content of the page which will contain the link to your login page)
application/views/login.php (this is the content of the login page)
application/config/routes.php (this will be used to reroute /login to /user/login)
So, now let's look at the code in each file that will achieve what you're trying to do. First the main.php controller (which, again, will be your default controller). This will be called when you go to your website's root address... www.example.com
application/controllers/main.php
class Main extends Controller
{
function __construct() //this could also be called function Main(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function index()
{
$this->load->view('header.php');
$this->load->view('splashpage.php');
$this->load->view('footer.php');
}
}
Now let's take a look at the header, footer, and splashpage views:
application/views/header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="This is the text people will read about my website when they see it listed in search engine results" />
<title>Yustme's Site</title>
<!-- put your CSS includes here -->
</head>
<body>
application/views/splashpage.php - note: there's no reason why you need a wrapper div here...it's only put there as an example
<div id="splashpagewrapper">
Click here to log in
</div>
application/views/footer.php
</body>
<!-- put your javascript includes here -->
</html>
And now let's look at the User controller and the login.php view:
application/controllers/user.php
class User extends Controller
{
function __construct() //this could also be called function User(). the way I do it here is a PHP5 constructor
{
parent::Controller();
}
function login()
{
$this->load->view('header.php');
$this->load->view('login.php');
$this->load->view('footer.php');
}
}
application/views/login.php
<div id="login_box">
<!-- Put your login form here -->
</div>
And then finally the route to make /login look for /user/login:
application/config/routes.php
//add this line to the end of your routes list
$routes['login'] = '/user/login';
And that's it. No magic or anything. The reason I brought up the fact that you can load views as strings is because you may not want to have separate "header" and "footer" views. In this case, you could simply "echo" out a view as a string INTO another view. Another example is if you have a shopping cart full of items and you want the shopping cart and the items to be separate views. You could iterate through your items, loading the "shoppingcartitem" view as a string for each item, concatenate them together, and echo that string into the "shoppingcart" view.
So that should be it. If you still have questions, please let me know.
Note that you are not specifying the correct method name for your main controller:
class Mainpage extends Controller
{
function Welcome() // < problem should be Mainpage
{
parent::Controller();
}
function index()
{
$this->load->view('mainpage.html');
}
}
Suggestion:
Instead of creating html page, create a php page and use the include command to include the login.php file.
Mainpage.php:
<HTML>
<HEAD>
<TITLE></TITLE>
<style>
a.1{text-decoration:none}
a.2{text-decoration:underline}
</style>
</HEAD>
<BODY>
<?php include './login.php'; ?>
</BODY>
</HTML>
And modify your main controller with this file name eg:
class Mainpage extends Controller
{
function Mainpage()
{
parent::Controller();
}
function index()
{
$this->load->view('mainpage.php');
}
}
I think you should add a file named .htaccess at first in your home folder
for example
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/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>
in the test field you need to add your homefolder or baseurl
and you need to set the baseurl in config page as
$config['base_url'] = 'http://localhost/test';
And the link inside the html page should be as
<a class="2" href="http://localhost/test/Login">login</a>
In login_form page you need to make action url as
http://localhost/test/Login/verify