404 not found on Codeigniter - codeigniter

I am trying to access my page 'login', and I'm using Codeingiter.
I'm using TAL php too, for templates.
I just defined my routes like this:
$route['default_controller'] = "site";
$route['404_override'] = '';
$route['login'] = 'login';
$route['signup'] = 'login';
$route['success'] = 'login';
And here is my 'login' controller :
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends CI_controller{
public function __construct(){
parent::__construct();
$this->tal->setOutputMode(PHPTAL::HTML5);
if($this->session->userdata('loggedIn') == true){
$this->tal->template = 'admin';
$this->tal->login = true;
}else{
$this->tal->template = 'main';
$this->tal->login = false;
}
$this->load->model('membership_model');
}
public function index(){
$requestPage = $this->uri->segment(1);
switch($requestPage){
case 'login':
$this->tal->title = 'Connexion';
$this->tal->display('login.php');
break;
case 'signup':
$this->tal->title = 'Inscription';
$this->tal->display('signup.php');
$this->createMember();
break;
case 'success':
$this->tal->title = 'Inscription validée!';
$this->tal->display('messagePage/signupSuccess.php');
break;
}
}
function validateCredentials(){
$query = $this->membership_model->validate();
if($query){
$userdata = array(
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'loggedIn' => true
);
$this->session->set_userdata($userdata);
redirect('backoffice/profile/#');
}
$this->index();
}
function createMember(){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Nom', 'trim|required');
$this->form_validation->set_rules('forename', 'Prénom', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('adress', 'Adresse', 'trim|required');
$this->form_validation->set_rules('zipcode', 'Code Postal', 'trim|is_natural|required');
$this->form_validation->set_rules('city', 'Ville', 'trim|required');
$this->form_validation->set_rules('country', 'Pays', 'trim|required');
$this->form_validation->set_rules('username', 'Pseudo', 'trim|required|callback_usernameCheck');
$this->form_validation->set_rules('password', 'Mot de passe', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Confirmation du mot de passe', 'trim|required|matches[password]');
$this->form_validation->set_rules('captcha', 'Captcha');
$data = array(
'firstname' => $this->input->post('name'),
'forename' => $this->input->post('forename'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'adress' => $this->input->post('adress'),
'zipcode' => $this->input->post('zipcode'),
'city' => $this->input->post('city'),
'country' => $this->input->post('country'),
'password' => $this->input->post('password'),
'passwordHashed' => md5($this->input->post('password'))
);
if($this->form_validation->run() == false){}
else{
$result = $this->membership_model->usernameCheck();
var_dump($result);
switch($result){
case false:echo 'false';
$this->form_validation->set_message('', 'Désolé mais ce nom d\'utilisateur et / ou cet email sont déjà pris');
break;
case true:echo 'true';
$this->membership_model->createMember($data);
redirect('success');
break;
}
}
}
function logout(){
$this->session->sess_destroy();
redirect('/');
}
}//this ends class
My local URL is : http://localhost/dist.
And when I try to access it, with the URL http://localhost/dist/login, I just get '404 Page Not Found : The page you requested was not found.'
I don't understand why. All is ready.

Try accessing the url http://localhost/dist/index.php/login. It doesn't appear that you have removed the index.php in the config file. Here is how to do that.

Have you tried debugging it?
If you don't have any routes defined, do you still get the 404 error?
I would bet that the issue is because you're routing a path to itself, but I don't know for sure.
Take out all of the routes and make sure that the path works. Then, add one at a time until it stops working.

Related

Codeigniter sessions, display user's firstname

I'm trying to display the user's firstname after login but it doesn't appear on the page. I tried to display it like this
<h2> Welcome <?php echo $this->session->userdata('firstname'); ?> </h2>
but it didn't display the user's firstname, even though there are data in DB. But when I tried to change the 'firstname' into 'username', it displayed the email of the user(test#gmail.com). How can I display the user firstname? Here's my controller:
function login(){
$this->form_validation->set_rules('username', 'Username', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE) {
redirect('/');
} else {
$user_login = array(
'username' => $this->input->post('username'),
'password' => $this->input->post('password'),
);
$login = new UI_model;
$result = $login->login_user($user_login);
if($result){
$auth_details = array(
'firstname' => $result->firstname,
'lastname' => $result->lastname,
'username' => $result->username,
);
$this->session->set_userdata($auth_details);
// $this->session->set_userdata('authenticated', '1');
$this->session->set_flashdata('status', 'User Logged in Successfully!');
redirect(base_url('pages/index'));
} else {
$this->session->set_flashdata('status', 'Invalid Credentials. Please try again');
redirect(base_url('pages/about'));
}
}
}
Here's my Model:
public function login_user($data){
$this->db->select('*');
$this->db->where('username', $data['username']);
$this->db->where('password', $data['password']);
$this->db->from('users');
$this->db->limit(1);
$query = $this->db->get();
if($query->num_rows() == 1){
return $query->row();
} else {
return false;
}
}
Can you attach an image of your database you are using for and echo all data session?

Codeigniter login error switch between login and welcome

Now I'm developing system documentation using Codeigniter. But the problem is Not logged in Problems switching between pages. login and Welcome To switch on like this forever.
public function index()
{
if($this->input->post(NULL)){
$username = $this->input->post('username');
$password = $this->input->post('password');
$result = $this->m_login->check_login($username,$password);
if(!empty($result)){
$data_account = array();
foreach($result as $row){
$data_account = array(
'account_id' => $row->id,
'username' => $row->user_name,
'password' => $row->password,
'fullname' => $row->fullname,
'email' => $row->email,
'tel' => $row->tel,
'detail' => $row->detail
);
}
$this->session->set_userdata('data_account',$data_account);
$this->__login();
//create_log_activity($this->account_id, date("Y-m-d H:i:s"));
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}else if($this->session->userdata('data_account')){
redirect('welcome', 'refresh');
exit;
}else{
$this->load->view('login');
}
}
public function logout(){
$this->session->unset_userdata('data_account');
redirect('','refresh');
}
I want to know how to solve the problem, thanks in advance.
Use
if($_SERVER['REQUEST_METHOD'] == "POST"){
instead of
if($this->input->post(NULL)){

Ion Auth - Create_User In Another Controller Does Not Work

I've taken the "auth" controller and copied it and renamed it as "site". I have renamed the references to views etc. to "site". When I go to www.mysite/index.php/site/create_user the form loads fine. However on hitting submit I get redirected to www.mysite.com/index.php/site/login and nothing is added to the database. Can anyone tell me why this does not work? My site controller is below:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends CI_Controller {
//
//Authentication
//
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->library(array('ion_auth','form_validation'));
$this->load->helper(array('url','language'));
$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
$this->lang->load('auth');
}
//Function to log the user in
function login()
{
$this->data['title'] = "Login";
//validate form input
$this->form_validation->set_rules('identity', 'Identity', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == true)
{
// check to see if the user is logging in
// check for "remember me"
$remember = (bool) $this->input->post('remember');
if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
{
//if the login is successful
//redirect them back to the home page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('/', 'refresh');
}
else
{
// if the login was un-successful
// redirect them back to the login page
$this->session->set_flashdata('message', $this->ion_auth->errors());
redirect('site/login', 'refresh'); // use redirects instead of loading views for compatibility with MY_Controller libraries
}
}
else
{
// the user is not logging in so display the login page
// set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['identity'] = array('name' => 'identity',
'id' => 'identity',
'type' => 'text',
'value' => $this->form_validation->set_value('identity'),
);
$this->data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
);
$this->_render_page('site/login', $this->data);
}
}
//Function to log the user out
function logout()
{
$this->data['title'] = "Logout";
// log the user out
$logout = $this->ion_auth->logout();
// redirect them to the login page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect('site/login', 'refresh');
}
//Function to create a user
function create_user()
{
$this->data['title'] = "Create User";
if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
{
//redirect('site/login', 'refresh');
}
$tables = $this->config->item('tables','ion_auth');
// validate form input
$this->form_validation->set_rules('first_name', $this->lang->line('create_user_validation_fname_label'), 'required');
$this->form_validation->set_rules('last_name', $this->lang->line('create_user_validation_lname_label'), 'required');
$this->form_validation->set_rules('email', $this->lang->line('create_user_validation_email_label'), 'required|valid_email|is_unique['.$tables['users'].'.email]');
$this->form_validation->set_rules('phone', $this->lang->line('create_user_validation_phone_label'), 'required');
$this->form_validation->set_rules('company', $this->lang->line('create_user_validation_company_label'), 'required');
$this->form_validation->set_rules('password', $this->lang->line('create_user_validation_password_label'), 'required|min_length[' . $this->config->item('min_password_length', 'ion_auth') . ']|max_length[' . $this->config->item('max_password_length', 'ion_auth') . ']|matches[password_confirm]');
$this->form_validation->set_rules('password_confirm', $this->lang->line('create_user_validation_password_confirm_label'), 'required');
if ($this->form_validation->run() == true)
{
$username = strtolower($this->input->post('first_name')) . ' ' . strtolower($this->input->post('last_name'));
$email = strtolower($this->input->post('email'));
$password = $this->input->post('password');
$additional_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'company' => $this->input->post('company'),
'phone' => $this->input->post('phone'),
);
}
if ($this->form_validation->run() == true && $this->ion_auth->register($username, $password, $email, $additional_data))
{
// check to see if we are creating the user
// redirect them back to the admin page
$this->session->set_flashdata('message', $this->ion_auth->messages());
redirect("site", 'refresh');
}
else
{
// display the create user form
// set the flash data error message if there is one
$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
$this->data['first_name'] = array(
'name' => 'first_name',
'id' => 'first_name',
'type' => 'text',
'value' => $this->form_validation->set_value('first_name'),
);
$this->data['last_name'] = array(
'name' => 'last_name',
'id' => 'last_name',
'type' => 'text',
'value' => $this->form_validation->set_value('last_name'),
);
$this->data['email'] = array(
'name' => 'email',
'id' => 'email',
'type' => 'text',
'value' => $this->form_validation->set_value('email'),
);
$this->data['company'] = array(
'name' => 'company',
'id' => 'company',
'type' => 'text',
'value' => $this->form_validation->set_value('company'),
);
$this->data['phone'] = array(
'name' => 'phone',
'id' => 'phone',
'type' => 'text',
'value' => $this->form_validation->set_value('phone'),
);
$this->data['password'] = array(
'name' => 'password',
'id' => 'password',
'type' => 'password',
'value' => $this->form_validation->set_value('password'),
);
$this->data['password_confirm'] = array(
'name' => 'password_confirm',
'id' => 'password_confirm',
'type' => 'password',
'value' => $this->form_validation->set_value('password_confirm'),
);
$this->_render_page('site/create_user', $this->data);
}
}
//Function to render the page
function _render_page($view, $data=null, $returnhtml=false)//I think this makes more sense
{
$this->viewdata = (empty($data)) ? $this->data: $data;
$view_html = $this->load->view($view, $this->viewdata, $returnhtml);
if ($returnhtml) return $view_html;//This will return html on 3rd argument being true
}
}
This exact code works when in the auth controller. When in the site controller I make it so you must login and you must be an admin to make a user (i.e. uncommenting out this line //redirect('site/login', 'refresh');) then it also works, but for some reason when that line is commented it works in the auth controller but not the site controller.
Any help is much appreciated. I've tried to figure it out but can't see why it works in one and not the other (and why it works in site but only as an admin when that code is uncommented and not at all when it is commented, whilst in auth it works in either case).
Thanks in advance.
The reason you get redirected is one of two reasons.
First : $this->_render_page('site/login', $this->data);
When you hit the submit button it is still pointing to the login controller.
Second : if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
The create user function in the Auth controller is for admins only, You will have to // out the code or you will be redirected to the login page due to not being logged and not being an admin.
try this:
//$this->_render_page('site/login', $this->data);
//if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
By marking out these two lines you should be able to veiw and submit your page without being redirected.
:)

How to disable direct access to callback functions?

<? if ( ! defined('BASEPATH')) exit();
class Registration extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('registration_model');
}
public function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|valid_email|callback_email_available');
if($this->form_validation->run() == FALSE) {
$this->load->view('registration');
} else {
$this->registration_model->add_user();
}
}
# Check E-mail
public function email_available($email) {
$this->db->select('email');
$this->db->where('email', $email);
$query = $this->db->get('users');
$result = $query->row();
if(!empty($result)) {
$this->form_validation->set_message('email_available', 'This e-mail belongs to another user.');
return FALSE;
} else {
return TRUE;
}
}
}
?>
I have a registration form with Form Validation.
And I have a callback function to validate email uniqueness.
All code works fine, but I can directly access to callback function with errors
examle.com/registration/email_available
A PHP Error was encountered
Severity: Warning
Message: Missing argument 1 for Registration::email_available()
Filename: controllers/registration.php
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: email
Filename: controllers/registration.php
How can I deny direct access to callback function?
You can prefix the method name with an _ to deny access through HTTP request.
My suggestion is to tuck your validation rules into a separate file. CodeIgniter supports this by allowing you to save validation configurations in config/form_validation.php. Take a look at the Form Validation Documentation, specifically the section labelled Saving Sets of Validation Rules to a Config File.
Your controller's index:
public function index() {
$this->load->library('form_validation');
if($this->form_validation->run('submit_registration') == FALSE) {
$this->load->view('registration');
}
else{
$this->registration_model->add_user();
}
}
config/form_validation.php
$config = array
(
'submit_registration' => array
(
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|email_available'
),
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'required|alpha_numeric|etc'
)
),
'some_other_submission' => array(
array(
'field' => 'somefield',
'label' => 'SomeField',
'rules' => 'some|rules'
),
array(
'field' => 'getit',
'label' => 'Get The Point?',
'rules' => 'trim'
)
)
);
libraries/MY_Form_validation.php
class MY_Form_validation extends CI_Form_validation
{
function __construct($config = array()){
parent::__construct($config);
}
function email_available($email){
$CI =& get_instance();
//check your database with the $CI variable...
if(email_exists) return TRUE;
else return FALSE;
}
}

CodeIgniter. Update where id problem

I'm trying to update a contact and only update the contact depending on the id.
Model:
function updateContact() {
$data = array(
'title' => $this->input->post('title'),
'first_name' => $this->input->post('first_name'),
'surname' => $this->input->post('surname'),
'phone_number' => $this->input->post('phone_number'),
'fax' => $this->input->post('fax'),
'email' => $this->input->post('email')
);
$this->db->where('id', $this->uri->segment(3));
$this->db->update('contacts', $data);
}
Controller:
function update() {
$this->load->model('league/add_contact_model');
$data['rows'] = $this->add_contact_model->viewContact();
// field name, error message, validation rules
$this->form_validation->set_rules('title', 'Title', 'trim|required');
$this->form_validation->set_rules('first_name', 'First Name', 'trim|required');
$this->form_validation->set_rules('surname', 'Surname Name', 'trim|required');
$this->form_validation->set_rules('phone_number', 'Phone Number', 'trim|required');
$this->form_validation->set_rules('email', 'Email', 'trim|required');
if($this->form_validation->run() == FALSE)
{
$data['main_content'] = "league/update_contact";
$this->load->view('includes/template', $data);
}
else
{
if($query = $this->add_contact_model->updateContact())
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/contact_updated');
$this->load->view('includes/footer');
}
else
{
$this->load->view('includes/top');
$this->load->view('includes/header');
$this->load->view('league/about_added');
$this->load->view('includes/footer');
}
}
}
It's updating if the where statement isn't there, but obviously it just updates everything, so just unsure of how the where statement should be written.
Thanks in advance for any help :)
function updateContact( $id ) {
....
$this->db->where('id', $id );
in your controller make sure that id (segment(3)) is valid before execute Model method
Just to add to bensius comment, you really shouldn't have $this->uri->segment(3) in the model. This should belong in your controller.

Resources