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';
Related
page error
My htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
My Controller
<?php
defined('BASEPATH') or exit('No direct script access allowed');
class Auth extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library('form_validation');
}
public function index()
{
$this->form_validation->set_rules('username', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Email', 'required|trim');
if ($this->form_validation->run() == FALSE) {
# code...
$data['title'] = 'Login';
$this->load->view('templates/auth_header', $data);
$this->load->view('auth/login');
$this->load->view('templates/auth_footer');
} else {
$this->_login();
}
}
anyone know my problem? I think my syntax is correct but the page is still not found.
anyone help me, I'm still a beginner in the field of programmers
Please add this htaccess file in application folder.It may helps
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
Your htaccess file is in outside the application folder.This was also needed.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
**First, you check, that the post data is checked if the condition or else condition inside echo and exit the post data.**
public function index()
{
$this->form_validation->set_rules('username', 'Email', 'required|trim');
$this->form_validation->set_rules('password', 'Email', 'required|trim');
if ($this->form_validation->run() == FALSE) {
echo " If stmt ";
exit();
# code...
$data['title'] = 'Login';
} else {
echo " else stmt";
exit();
$this->_login();
}
}
Please change your file name like (auth/Login) first letter capital
I have a CodeIgniter project on a subdomain. Now, When I visit sub.example.com it loads a login page and on successful login, it redirects to dashboard. Once logged in and session are in place visiting sub.example.com/login/ will auto-redirect to the dashboard page. Now here's my problem. After successful login, visiting sub.example.com doesn't redirect anywhere it simply load the login page. But visiting sub.example.com/index.php does redirect me to the dashboard page.
For some reason, my index method is called or working properly.
Here my code.
.htaccess
IndexIgnore *
php_value date.timezone Asia/Kolkata
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
## Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://sub.example.com/$1 [R=301,L]
## Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
config.php
if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
$config['base_url'] = "http://" . $_SERVER['SERVER_NAME'];
} else {
$config['base_url'] = "https://" . $_SERVER['SERVER_NAME'];
}
routes.php
$route['default_controller'] = 'root';
$route['root_controller'] = 'root';
/*API CONTROLLER*/
$route['api_controller'] = 'api';
/*Guest Controller*/
$route['guest_controller'] = 'guest';
/*Custom Routes*/
$route['dashboard/(:any)'] = 'root/dashboard/$1';
$route['search/(:any)'] = 'root/search/$1';
$route['search/(:any)/(:num)'] = 'root/search/$1/$2';
$route['export/(:any)'] = 'root/export/$1';
root controller
public function __construct()
{
parent::__construct();
$this->default_data = array("project_name" => PROJECT_NAME);
if ($this->router->method == "privacy_policy") {
return;
}
$this->load->library('session');
if ($this->router->method == "login") {
if ($this->session->userdata("username")) {
redirect(base_url("dashboard/"));
}
} else {
if (!$this->session->userdata("username")) {
redirect(base_url("login/"));
}
}
//Set MYSQL timezone
$this->db->query("SET time_zone = '+05:30'");
}
/**
* Dashboard View
*/
public function index()
{
redirect(base_url("/dashboard/"));
}
/**
* Login View
*/
public function login()
{
$data = $this->default_data;
if ($_POST) {
$username = $this->input->post("username");
$plain_password = $this->input->post("password");
$this->load->model("authenticate");
if (!$this->authenticate->auth($username, $plain_password)) {
$data['message'] = "Invalid Credentials";
}
}
$this->load->view('login', $data);
}
Update
Forgot to mention that I am having this issue only on the remote server. On localhost it's working fine.
Hers is an idea - add this after you have loaded the session object:
if ($this->session->userdata("username") && $this->router->method == "index") {
redirect(base_url("dashboard/"));
}
Please use this .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
if you visiting sub.example.com/index.php successfully then try this
public function login()
{
$data = $this->default_data;
if ($_POST) {
$username = $this->input->post("username");
$plain_password = $this->input->post("password");
$this->load->model("authenticate");
if (!$this->authenticate->auth($username, $plain_password)) {
$data['message'] = "Invalid Credentials";
}else{
//go to dashboard function
$this->index();
}
}
$this->load->view('login', $data);
}
Turns out there was some issue with files on the remote server. I don't exactly know if the files were different or corrupted but replacing the whole project on the remote server fixed the issue
"Turns out there was some issue with files on the remote server. I don't exactly know if the files were different or corrupted but replacing the whole project on the remote server fixed the issue" --
#Akash You should compare your local and remote .htaccess files. I think that will explain your problem.
I am using codeIgniter 3.x. I load a view containing a form that submits to User.php controller but whenever I hit the submit button it redirect to this url "http://localhost/signup/Users/register" and it always gives "Object not found! The requested URL was not found on this server."
following is my .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /signup/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /signup/index.php
</IfModule>
following is autload helper in autload.php
$autoload['helper'] = array('url','form');
following is config.php
$config['base_url'] = 'http://localhost/signup/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';
following is how I open a form and close it in Welcome.php view.
<?php echo form_open("Users/register", $attributes);?>
<?php echo form_close(); ?>
Following is Users.php controller
class Users extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form','url'));
$this->load->library(array('session', 'form_validation', 'email'));
$this->load->database();
$this->load->model('user_model');
$this->load->view('user_registration_view');
}
function index()
{
$this->register();
}
function register()
{
//set validation rules
$this->form_validation->set_rules('fname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('lname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[user.email]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|md5');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]|md5');
//validate form input
if ($this->form_validation->run() == FALSE)
{
// fails
$this->load->view('user_registration_view');
}
else
{
//insert the user registration details into database
$data = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
// insert form data into database
if ($this->user_model->insertUser($data))
{
// send email
if ($this->user_model->sendEmail($this->input->post('email')))
{
// successfully sent mail
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
redirect('user/register');
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user/register');
}
}
else
{
// error
$this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user/register');
}
}
}
The problem with your htaccess so change it to default
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
And in config
$root=(isset($_SERVER["HTTPS"]) ? "https://" : "http://").$_SERVER["HTTP_HOST"];
$root.= str_replace(basename($_SERVER["SCRIPT_NAME"]), "", $_SERVER["SCRIPT_NAME"]);
$config["base_url"] = $root;
If your application is under your signup folder ( as it appears by your .htaccess file ) then you need to do the following...
.htaccess
This lives in the same folder as your application. i.e the same folder as the system index.php file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
As your URL is http://localhost/signup/ which you have set correctly in your $config['base_url'] that should work.
I just created a dummy test site and tested it.
Let me know if that works.
Filename should be same as class name Users.php Class Users extends CI_Controller
I think you made mistake while redirecting ...
replace :
redirect('user/register');
with :
redirect('Users/register'); OR redirect('Users/register','refresh');
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!
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’);