There seems to be many questions about form validation but none answer my problem.
Unless I'm missing something obvious, my form validation will not run, but only on a certain page, all other pages are fine.
Here is the problem code:
public function activate($code = '')
{
// This function lets a user activate their account with the code or link they recieved in an email
if($code == '' || isset($_POST[''])){
$form = '';
$this->load->library('form_validation');
$this->load->helper('form');
if ($this->form_validation->run() == FALSE){
// No code, so display a box for them to enter it manually
$this->form_validation->set_rules('activation_code', 'Activation Code', 'trim|required|xss_clean|integer');
$form .= validation_errors();
$form .= form_open_multipart('user/activate', array('class' => 'formee'));
$form .= form_label('Enter your activation code below and click \'Activate\' to start using your account. If you need a hand please contact live chat.', 'activation_code');
$form .= form_input(array('name' => 'activation_code'));
$data = array(
'name' => 'submit',
'value' => 'Activate',
'class' => 'right',
'style' => 'margin-top:10px;',
);
$form .= form_submit($data);
$form .= form_close();
}else{
$form .= "form run";
}
}else{
// Code recieved through the GET or POST variable XSS clean it and activate the account
}
$data = array(
'title' => $this->lang->line('activate_title'),
'links' => $this->gen_login->generate_links(),
'content' => $form
);
$this->parser->parse('beer_template', $data);
}
You can see the page here: http://77.96.119.180/beer/user/activate
You can see form validation working here: http://77.96.119.180/beer/user/register
Can anyone help?
You call the set_rules after the run:
if ($this->form_validation->run() == FALSE){
// No code, so display a box for them to enter it manually
$this->form_validation->set_rules('activation_code', 'Activation Code', 'trim|required|xss_clean|integer');
But should be before:
$this->form_validation->set_rules('activation_code', 'Activation Code', 'trim|required|xss_clean|integer');
if ($this->form_validation->run() == FALSE){
}
Related
I want to insert validate data into database.
My code is like below, where submit is my button name and name, email, mobile is my form fields:
if($this->input->post('submit'))
{
$name = $this->input->post('name');
$email = $this->input->post('email');
$mobile = $this->form_validation->set_rules('mobile', 'Mobile','trim|required|numeric');
$query = $this->my_model->insertdata($name, $email, $mobile);
}
How can I validate form fields if I have written code like above?
You should use Form Validation Library.
First, autoload it:
Open application/config/autoload.php and edit line 61 like below:
$autoload['libraries'] = array('form_validation');
Now, you can validate your form in your controller. Here is an example for you:
public function some_function()
{
$this->form_validation->set_rules('input_name', 'input human name', 'required');
if ($this->form_validation->run() === false)
{
//return some error here
}
else
{
// send data to your model (e.g. $this->Some_model->some_function())
}
}
Please see Form Validation Library Guide to see all references and examples. Also please follow this order for submitting forms: View -> Controller -> Model.
first, you are putting insecure data into your database.
$data = [
'name' => html_escape($this->input->post('name'),
'email' => html_escape($this->input->post('email'),
'mobile' => html_escape($this->input->post('mobile');
];
$this->form_validation->set_rules('mobile',Mobile','trim|required|numeric');
$this->form_validation->set_rules('name',name','trim|required');
$this->form_validation->set_rules('email',Email','trim|required|valid_email');
if ($this->form_validation->run() === false)
{
//print error
}else
{
$this->db->insert('TABLE', $data);
}
public function register()
{
$this->form_validation->set_rules('username', 'Usename Name', 'trim|required|min_length[3]|max_length[30]');
$this->form_validation->set_rules('email', 'Email ID', 'trim|required|valid_email|is_unique[jobseeker_registration.email]');
$this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|numeric');
if ($this->form_validation->run() === false)
{ $this->load->view('register'); }
else
{
$data = array(
'name' => $this->input->post('name'),
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'mobile' => $this->input->post('mobile'),
'password' => $this->input->post('password')
);
$this->Jobseeker_model->insertdata($data)
}
}
This code is work for me very well for form validation
I test on my localhost is working well and now show any error but after upload to the godaddy server. it is codeigniter version 2.2.6.
this error show in the login page.
Error Message 1
Message: Cannot modify header information - headers already sent by (output started at /home/user/public_html/ngapali/test/application/controllers/admin/user.php:1)
Filename: libraries/Session.php
Line Number: 433
Error Message 2
Message: Cannot modify header information - headers already sent by (output started at /home/user/public_html/ngapali/test/application/controllers/admin/user.php:1)
Filename: libraries/Session.php
Line Number: 689
Admin Controller of My Controller
<?php
class Admin_Controller extends MY_Controller{
function __construct(){
parent::__construct();
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->library('session');
$this->load->model('user_m');
$this->load->model('roomtype_m');
$this->load->model('room_m');
$this->load->model('roomcapacity_m');
//login check
$exception_uris = array(
'admin/user/login',
'admin/user/logout'
);
if(in_array(uri_string(), $exception_uris) == FALSE) {
if($this->user_m->loggedin() == FALSE) {
redirect('admin/user/login');
}
}
}}?>
My login page /user/login
Controller
<?php
class User extends Admin_Controller {
function __construct(){
parent::__construct();
}
public function index() {
$this->data['users'] = $this->user_m->get();
$this->data['subview'] = 'admin/user/index';
$this->load->view('admin/_layout_main',$this->data);
}
public function login() {
$dashboard = 'l0gadmin/dashboard';
$this->user_m->loggedin() == FALSE || redirect($dashboard);
$rules = $this->user_m->rules;
$this->form_validation->set_rules($rules);
if($this->form_validation->run() == TRUE){
if($this->user_m->login() == TRUE){
redirect($dashboard);
}else{
$this->session->set_flashdata('error','That email/password combination does not exist ');
redirect('admin/user/login','refresh');
}
}
$this->data['subview'] = 'admin/user/login';
$this->load->view('admin/_layout_modal',$this->data);
}
public function logout(){
$this->user_m->logout();
redirect('admin/user/login');
}}?>
Model
<?php
class User_m extends MY_Model {
function __construct(){
parent::__construct();
}
public function login(){
$user = $this->get_by(array(
'email'=> $this->input->post('email'),
'password' => $this->hash($this->input->post('password')),
),TRUE);
if(count($user)){
$data = array(
'name'=> $user->name,
'email' => $user->email,
'id'=> $user->id,
'loggedin'=>TRUE,
);
$this->session->set_userdata($data);
}
}
public function logout(){
$this->session->sess_destroy();
}
public function loggedin(){
return (bool) $this->session->userdata('loggedin');
}
public function hash($string){
return hash('sha512', $string . config_item('encryption_key'));
}}?>
View Page of login
<div class="row">
<div class="col-md-3">
<h2>Admin Login</h2>
<?php
echo validation_errors();
echo form_open('');
$email = array(
'name' => 'email',
'class' => 'form-control',
'placeholder' => 'Email Address'
);
echo form_input($email) . "<br/>";
$password = array(
'name' => 'password',
'class' => 'form-control',
'placeholder' => 'Password'
);
echo form_password($password) . "<br/>";
$submit = array(
'name' => 'submit',
'class' => 'form-control btn btn-primary',
'value' => 'Login'
);
echo form_submit($submit);
echo form_close();
?>
</div>
</div>
i search this problem can be of open and closed tag. I try to add all the page but it still show error. Why?.
Read the error message:
output started at
/home/user/public_html/ngapali/test/application/controllers/admin/user.php:1
/user.php file has some blank character on line 1
check for any blank space before <?php tag.
Remove it and refresh error will be go away.
One more suggestion,
Omit ending php tags ?> in controller and model files.
Read more here : Why would one omit the close tag?
This is most probably because there occured an error or warning, which is not visible because of your current display_errors setting. I would suggest turning it on in order to fix these errors/warnings.
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.
:)
I'm having trouble with a form. I've tested it many times and it seems that the problem lies on a string with http:// . Whenever I input www.mywebsite.com it runs but as soon as I input http://mywebsite.com the form doesn't run. Anyone can help??
controller (add.php)
public function add_validation(){
$this->load->library('form_validation');
$this->form_validation->set_rules('add', 'Add', 'required|trim');
if ($this->form_validation->run()){
echo "The form validation ran";
//redirect('main/members');
}else{
echo "The form validation didn't ran";
//redirect('main/members');
}
}
view (header.php)
echo form_open('add/add_validation');
//echo validation_errors();
$text = array(
'name' => 'add',
'id' => 'text',
'placeholder' => 'http://'
);
echo form_input($text);
$submit = array(
'name' => 'add_submit',
'id' => 'submit',
'value' => '+',
);
echo form_submit($submit);
echo form_close();
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.