Codeigniter multiple URL's while running form_validation->run() - php

I have a register_page.php file in the views folder which is just a register form. When you click the register button, and say the password doesn't match, it should that the password doesn't match after clicking the submit button. However, after you type the password and it still doesn't match, it doesn't do anything, it just duplicates the url.
For example
URL when the password doesn't match: http://localhost/dayone/user/register_user
URL when when the password still doesn't match: http://localhost/dayone/user/user/register_user
At this point, the form is empty, all the values are removed and when you press enter without filling anything in, it doesn't show any error, but the URL says: http://localhost/dayone/user/user/register_user
What's causing this?
My user.php controller:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index()
{
$this->load->view('includes/header');
$this->load->view('register_page');
$this->load->view('includes/footer');
}
public function register_user () {
$this->load->library('form_validation');
//rules to become a registered user
$this->form_validation->set_rules('first_name', 'First Name', 'required|trim|min_length[3]|max_length[20]|xss_clean');
$this->form_validation->set_rules('last_name', 'Last Name', 'required|trim|min_length[3]|max_length[20]|xss_clean');
$this->form_validation->set_rules('email', 'Email', 'required|trim|min_length[6]|max_length[50]|valid_email|is_unique[users.email]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[6]|max_length[50]|matches[password_conf]|xss_clean');
$this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|trim|min_length[6]|max_length[50]|xss_clean');
if ($this->form_validation->run() == FALSE) {
//user didn't validate, send back to login form and show errors
$this->load->view('includes/header');
$this->load->view('register_page');
$this->load->view('includes/footer');
} else {
//successful registration
$this->load->view('login');
}
}
}
My resister_page.php (with the register form):
<!DOCTYPE html>
<html lang="en">
<body>
<div id="container" class="page">
<div>
<section class="container">
<h2 class="block-title block-title--bottom">Login</h2>
<div class="login">
<?php echo validation_errors('<p class="alert-market">'); ?>
<form class="contact" id="contact-form" name="contact-form" method="post" action="user/register_user">
<!--- FIRST NAME --->
<input class="contact__field" value="<?php echo set_value('first_name'); ?>" name="first_name" id="first_name" type="text" placeholder="First Name">
<!--- LAST NAME --->
<input class="contact__field" value="<?php echo set_value('last_name'); ?>" name="last_name" id="last_name" type="text" placeholder="Last Name">
<!--- EMAIL --->
<input class="contact__field" value="<?php echo set_value('email'); ?>" name="email" id="email" type="email" placeholder="Email">
<!--- PASSWORD --->
<input class="contact__field" name="password" id="password" type="password" placeholder="Password">
<!--- CONFIRM PASSWORD --->
<input class="contact__field" name="password_conf" id="password_conf" type="password" placeholder="Confirm Password">
<a class="login__callback" href="#">Forgot password?</a>
<input class="btn btn--decorated btn-warning login__btn" value = "Login" name="submit" type="submit">
<?php echo form_close(); ?>
</div>
</section><!-- end container -->
</div>
</div><!-- /#page -->
</body>
</html>
I have no idea what is causing this.

Instead of this one:
<form class="contact" id="contact-form" name="contact-form" method="post" action="user/register_user">
You can use this one:
<?php echo form_open('user/register_user');?>
Which is also equal to this:
<form method="post" accept-charset="utf-8" action="http://localhost/dayone/user/register_user"/>
Assuming that your base_url in your config.php is equal to:
$config['base_url'] = 'http://localhost/dayone/';
I suggest you invest time in reading more about CodeIgniter's User Guide here.
EDIT:
Do not forget to load your form and url helpers too. You can load them in autoload.php in application/config
$autoload['helper'] = array("url","form");
Or
Make a function __contruct() and load your helpers and models there.
Example:
function __construct()
{
parent::__construct();
$this->load->model('sample_model');
$this->load->helper('url');
$this->load->helper('form');
}
Constructors are useful if you need to set some default values, or run
a default process when your class is instantiated. Constructors can't
return a value, but they can do some default work.
--From CodeIgniter - Controllers under Class Constructors

First use form helper to form open
<?php echo form_open('user/register_user');?>
Then change your match password rules like(match confirm password to password):-
$this->form_validation->set_rules('password', 'Password', 'required|trim|min_length[6]|max_length[50]|xss_clean');
$this->form_validation->set_rules('password_conf', 'Confirm Password', 'required|trim|min_length[6]|max_length[50]|matches[password]|xss_clean');
Then your else part should be a redirect not load a view again like
else {
// success register
redirect('login');
}

Related

Html disappear after using form_error function in codeigniter

I am trying to add the form error messages in my html form. Problem is there in first view. In user function html is disappear from where I start to use form_error(). My Original form design is like this: my original form
but after adding the form_error under the first input: my form error image
Here is my html code
<div class="form-group">
<label for="name"><i class="zmdi zmdi-account material-icons-name"></i></label>
<input type="text" name="username" id="name" placeholder="Your Name" />
<?php echo form_error('username','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>
<div class="form-group">
<label for="email"><i class="zmdi zmdi-email"></i></label>
<input type="text" name="email" id="email" placeholder="Your Email"/>
<?php echo form_error('email','<p class="p_errror" id="name_error_p">','</p>') ?>
</div>
Here is my controller
<?php
class Register extends CI_Controller
{
public function user()
{
$this->load->view('files/registration/signup');
}
public function login()
{
$this->load->view('files/registration/login');
}
public function description()
{
$this->load->view('files/registration/description');
}
public function registerMe()
{
$this->load->helper('form','url');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required|alpha|max_length[15]');
$this->form_validation->set_rules('pass', 'Password', 'required',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('re_pass', 'Confirm Password', 'required|matches[pass]');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
if ($this->form_validation->run()===FALSE) {
$this->load->view('files/registration/signup');
}
else
{
$this->load->model('RegisterUser');
$this->RegisterUser->registerme($_POST);
}
}
}
?>
you need to load the form helper in signup function too. form_error is a function in form_helper. (this is required for other functions with a form view too)
public function user()
{
$this->load->helper('form');
$this->load->view('files/registration/signup');
}
html is disappeared because there was php error. check your error log when this happens or change codeigniter environment to development in index.php

Validating a form hiding controller from URL Codeigniter

I'm facing a problem when trying to create a simple login system. This is how it works and shows what my problem is.
The login page contains a simple form, and opens a form to the controller 'Verify_login'. This Verify_login controller does some basic form validations, and then queries the database to check if the user entered correct credentials. When this form is submitted, it automatically directs the user to website.com/verify_login, while I would like it to remain website.com/login. I am not able to solve this problem using the routes file, because this causes the two pages to be both redirected to the same page. Does anyone have a solution to this? I'm using the Codeigniter framework.
login.php
<div class="content-block">
<h1>Login</h1>
<div class="content-inner-block">
<?php echo validation_errors(); ?>
<?php echo form_open('Verify_login'); ?>
<div class="login wrap">
<input type="text" name="username" id="user" placeholder="Username">
<input type="password" name="password" id="pass" placeholder="**********">
<input type="submit" value="Log in">
</div>
<?php echo form_close("\n")?>
</div>
Verify_login.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Verify_login extends CI_Controller{
public function index(){
$this->load->model('User_model');
$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('pages/login');
return false;
}
$result = $this->User_model->login($this->input->post('username'), $this->input->post('password'));
if($result){
echo 'loggedin';
} else {
echo 'wrong';
}
}
}
?>

Can't use Ion Auth's login method in another controller

I added controllers/Site.php , views/site/login.php to my project but when I submit login form i always get */application/views/site/login#
Do anyone knows why? whats the problem?
here is my Site.php :
<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Site extends MY_Controller {
function __construct() {
parent::__construct();
$this->load->library('ion_auth');
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->helper('string');
$this->load->database();
$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');
$this->load->helper('language');
$this->data['pageTitle'] = $this->lang->line('login_page_title');
}
//redirect if needed, otherwise display the user list
function index() {
if (!$this->ion_auth->logged_in()) {
//redirect them to the login page
redirect('site/login', 'refresh');
} else if (!$this->ion_auth->is_admin()) { //remove this elseif if you want to enable this for non-admins
//redirect them to the home page because they must be an administrator to view this
return show_error('You must be an administrator to view this page.');
}
}
//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
}
}
$this->_render_page('site/login', $this->data);
}
}
And here is my simple login.php :
<!doctype html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Login</h1>
<form role="form" action="#" method="post">
<div class="input-group">
<input type="email" id="identity" name="identity" placeholder="Your email address" value="admin#mail.com">
</div>
<div class="input-group">
<input type="password" id="password" name="password" placeholder="Your password" value="password">
</div>
<div class="checkbox-group">
<input type="checkbox" value="1" id="remember" name="remember" data-toggle="checkbox">
<label for="remember">Remember Me</label>
</div>
<button type="submit">Log me in</button>
</form>
</body>
</html>
This action="#" looks very strange. I think you should change this with action='/site/login'. You don't need use base_url() as commented above. Related path should be enough.

Codeigniter can't login

Here is the controller, when I click the login button, nothing happens. What I want is to load the success screen when user data is validated and show error messages when user data is not validated.
I have set my base_controller as Login
<?php
class Login extends CI_Controller {
/**
*
* load the magazines
*/
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->model('User','National_Holiday','Updated_Holiday');
}
public function index() {
$this->load->view('login');
}
/**
*
* add a magazine
*/
public function login(){
$this->form_validation->set_rules(array (
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required',
) ,
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric',
),
));
$this -> form_validation ->set_error_delimiters('<div class="alert alert-error">','</div>');
if(!$this->form_validation->run()){
$this->load->view('login');
}
else {
$this->load->view('national_holiday_screen');
}
}
}
here is the view
<?php echo validation_errors(); ?>
<form method="post">
<!-- LOGIN DIV STARTS HERE -->
<div>
<div> <h2> Log In </h2></div>
<div>
<lablel for="username"> Username </label>
<input type="text" name="username" value=""/>
</div>
<div>
<label for="password"> Password </label>
<input type="password" name="password" value=""/>
</div>
<div>
<br>
<input type="submit" value="Login">
</div>
</div>
<!-- LOGIN DIV ENDS HERE -->
</form>
When I click the login button, nothing happens. What am I doing wrong?
You have to give action attribute in form tag, like this:
action="http://yoursitename.com/controllername"
in your case controllername is login.
For more help, you can refer:
[https://ellislab.com/codeigniter/user-guide/helpers/form_helper.html][1]
Hope this help!
this link shows how form_open() works, one of codeigniter's utility functions from the form helper libary.
In your case you would want this line of code:
<?php echo form_open('/login'); ?>
or something that you want to be the login url.
This line would replace
<form method="post">
in your html and when rendered would be something like
<form method="post" accept-charset="utf-8" action="http:/example.com/index.php/login" />
If you aren't familiar with URI routing, then you should read about that here](https://ellislab.com/codeigniter/user-guide/general/routing.html). I would recommedn setting up a route for you login, but the default format for a url is
example.com/class/function/id/
so yours might look like
example.com/login/login
And form_open() would then look like (even though its kind of cluncky)
<?php echo form_open('/login/login'); ?>
You can try below code
login.php controller file
<?php
class Login extends CI_Controller {
/**
*
* load the magazines
*/
function __construct(){
parent::__construct();
$this->load->library('form_validation');
$this->load->helper('url');
$this->load->model('User','National_Holiday','Updated_Holiday');
}
public function index() {
$this->load->view('login');
}
/**
*
* add a magazine
*/
public function validate(){
$this->form_validation->set_rules(array (
array(
'field' => 'username',
'label' => 'username',
'rules' => 'required',
) ,
array(
'field' => 'password',
'label' => 'password',
'rules' => 'required|is_numeric',
),
));
$this -> form_validation ->set_error_delimiters('<div class="alert alert-error">','</div>');
if(!$this->form_validation->run()){
$this->load->view('login');
}
else {
$this->load->view('national_holiday_screen');
}
}
}
Here I have loaded one helper to give action url in view file
$this->load->helper('url');
Also I have changed function name from login to validate as function name should not be similar to class name as constructor is already defined there
login.php view file
<?php echo validation_errors(); ?>
<form method="post" action = "<?php echo site_url("login/validate"); ?>">
<!-- LOGIN DIV STARTS HERE -->
<div>
<div> <h2> Log In </h2></div>
<div>
<lablel for="username"> Username </label>
<input type="text" name="username" value=""/>
</div>
<div>
<label for="password"> Password </label>
<input type="password" name="password" value=""/>
</div>
<div>
<br>
<input type="submit" value="Login">
</div>
</div>
<!-- LOGIN DIV ENDS HERE -->
</form>
Hope this will help you

codeigniter setting up a password!

I try to set up a password in a codeigniter form...
Everything seems ok to my eyes but no matter which password I use the form is still submitted...
here is the code in the controler:
class MyBlog extends Controller{
function MyBlog(){
parent::Controller();
$this->load->helper(array('url','form','html')); //here we load some classes that we use
$this->load->scaffolding('entries'); //scaffolfing is a feature that lets you add or remove elements from the database
$this->load->scaffolding('comments');
$this->load->library('form_validation');//load validation class used to validate our forms...
}
function index(){
$data['title'] = "My Blog Title"; //the title of my blog
$data['query'] = $this->db->get('entries'); //here we make a small query to entries table
$this->load->view('myBlog_view', $data); ///load all data variables on myBlog_view.php
//this is also for the form validation
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('body', 'Body', 'required');
$this->form_validation->set_rules('author', 'Author', 'required');
$this->form_validation->set_rules('pass', 'Pass', 'callback_pass_check');
function pass_check($str) {
if ($str == 'baywatch'){
return TRUE;
}
else{
return FALSE;
}
}
if ($this->form_validation->run() == TRUE)
{
$this->myBlog_insert();
//$this->load->view('formSuccess_view');
}
}
function myBlog_insert(){
$insert = array( 'title' => $_POST['title'],
'body' => $_POST['body'],
'author' => $_POST['author']
);
$this->db->insert('entries',$insert);
redirect('myBlog/');
}
}
and this is my form:
<div class="theForm">
<?php echo $this->form_validation->error_string; ?>
<?php echo validation_errors(); ?>
<?php echo form_open('myBlog'); ?>
<label for="title">Title:</label>
<input type='text' name="title" size="40" id="title" />
<p>
<label for="body">Body:</label>
<textarea name="body" rows = "10" cols="60" id="body"></textarea>
</p>
<p>
<label for="author">Author:</label>
<input type="text" name="author" size="40" id="author"/>
</p>
<p>
<label for="pass">Password:</label>
<input type="password" name="pass" size="38" id="pass"/>
</p>
<p><input type="submit" value="Submit New Post"/></p>
</form>
</div>
</body>
</html>
any ideas?
thanks in advance
<label for="pass">Password:</label>
<input type="text" name="pass" size="38" id="author"/>
The input type is text no password, the id='pass'.
Ok, a couple of things first:
1) id's should be unique. ie your author field and your password field shouldn't have the same id.
2) password fileds should use the type "password" not "text".
I think the reason you're having problems is with your callback function pass_check(). Try changing your function to:
function pass_check($pass)
{
if($pass !== 'baywatch')
{
return FALSE;
}
By the way, scaffolding has now been deprecated. Can I suggest you look into using models and the active record class as a way of interacting with your db? Also, this really isn't a very secure way of handling passwords. Have a look at some of the CI authentication libraries and see if you can implement one of them.
Ok guys...I found what the problem was...function pass_check was declared inside index()...and for some reason it needs to be outside as a method of the class...Hope this will help others... I give some ups for all the suggestions...

Categories