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.
Related
I've got a form in Codeigniter 4 and I'm using the form helpers to build the form.
When I use a form_input I can reload the submitted values of the form using the old() method. Like below.
<span class="input-group-text" id="inputGroup-sizing-sm">Membership Number</span>
<?php
$current_membership_options = [
'id' => "membership_number",
'name' => "membership_number",
'class' => "form-control",
'type' => 'text'
];
echo form_input($current_membership_options, old('membership_number'))
?>
I've tried a couple of different options but I can't get it to repopulate with a form_dropdown.
THE VIEW
<span class="input-group-text" id="inputGroup-sizing-sm">Membership Type</span>
<?php
$membership_type_option = [
'id' => 'membership_type',
'name=' => 'membership_type',
'class' => 'form-select ',
'type' => 'text'
];
echo form_dropdown($membership_type_option, $membership_type, old('membership_type'));
?>
I get $membership_type from the user controller.
public function new(): string
{
$user = new User;
$data = $this->model->getMemberships();
return view('Admin/Users/new', [
'user' => $user,
'membership_type' => $data
]);
}
And the model
/**
* #throws Exception
*/
public function getMemberships(): array
{
$result = $this->db->query('select * from membership_type')->getResultArray();
$dropdown = array();
$dropdown['0'] = 'Please Select';
foreach ($result as $r)
{
$dropdown[$r['id']] = $r['type'];
}
return $dropdown;
}
Thanks in advance.
I just finished creating website for my school projects with CI 3. I've also made a session and also all the menu for admin and user. But, when i tried to register a new user account and then login with it, the page showing the exact data like user 1. How can i make a different crud database to every different user (with the same views of page), so every user can do CRUD on their data.
This is my authh controller code :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Authh extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->library('form_validation');
}
public function index(){
$this->form_validation->set_rules('email','Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password','Password', 'trim|required');
if($this->form_validation->run() == false){
$data['title']= 'LCA Master Login';
$this->load->view('auth/header2',$data);
$this->load->view('authh');
$this->load->view('auth/footer2');
} else{
$this->_login();
}
}
private function _login()
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$user = $this->db->get_where('user',['email' => $email])->row_array();
//jika usernya ada
if($user) {
// jika usernya aktif
if($user['is_active']==1){
//cek password
if(password_verify($password, $user['password'])){
$data= [
'email' => $user['email'],
'role_id' => $user['role_id']
];
$this->session->set_userdata($data);
if($user['role_id']== 1){
redirect('admin');
} else {
redirect('user');}
}else {
$this->session->set_flashdata('message','<div class="alert alert-danger" role="alert">Wrong password!</div>');
redirect('authh');
}
} else{
$this->session->set_flashdata('message','<div class="alert alert-danger" role="alert">Email has not been activated!</div>');
redirect('authh');
}
} else {
$this->session->set_flashdata('message', '<div class="alert alert-danger" role="alert">Email is not registered!</div>');
redirect('authh');
}
}
public function registration(){
$this->form_validation->set_rules('name', 'Name', 'required|trim');
$this->form_validation->set_rules('email', 'Email','required|trim|valid_email|is_unique[user.email]',[
'is_unique'=> 'This Email is already Registered!'
]);
$this->form_validation->set_rules('password1', 'Password', 'required|trim|min_length[3]|matches[password2]', [
'matches' => 'Password dont match!',
'min_length' => 'Password too short!'
]);
$this->form_validation->set_rules('password2', 'Password', 'required|trim|matches[password1]');
if ($this->form_validation->run() == false){
$data['title']= 'LCA Master Registration';
$this->load->view('auth/header2',$data);
$this->load->view('register');
$this->load->view('auth/footer2');
} else{
$data = [
'name' => htmlspecialchars($this->input->post('name', true)),
'email' => htmlspecialchars($this->input->post('email', true)),
'image' => 'default.jpg',
'password' => password_hash($this->input->post('password1'), PASSWORD_DEFAULT),
'role_id' => 2,
'is_active' =>1,
'date_created'=>time()
];
$this->db->insert('user', $data);
$this->session->set_flashdata('message','<div class="alert alert-info" role="alert">
Your Account Has Been Created! Please Login </div>');
redirect ('authh');
}
}
public function logoutt()
{
$this->session->unset_userdata('email');
$this->session->unset_userdata('role_id');
$this->session->set_flashdata('message','<div class="alert alert-success"
role="alert">You have been logged out!</div>');
redirect('authh');
}
}
i'm trying to make a login system. When user already login it will show user personal data like name, email or something. I'm try to modify the login code but doesn't work. Maybe someone can help.
Here is my code.
Controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User_login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Model_user');
$this->load->library('form_validation');
}
public function index()
{
$this->load->view('user/user_login');
}
function aksi_login(){
$username = $this->input->post('username');
$password = $this->input->post('password');
$where = array(
'username' => $username,
'password' => $password,
'status' => 1
);
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
}else{
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
}
function logout(){
$this->session->sess_destroy();
redirect('user/user_login');
}
}?>
Model
function cek_login($table,$where){
return $this->db->get_where($table,$where);
}
I'm can make username session appear,
<?php echo $this->session->userdata('username') ?> it's working.
But try to show name it doesnt appear. Appreciate any kind help. Thank you
You can try something like this.
$query = $this->Model_user->cek_login('tb_user',$where);
if($query->num_rows() > 0){
$user = $query->row();
$data_session = array(
'username' => $username,
'status' => "userlogin",
'name' => $user->name,
);
$this->session->set_userdata($data_session);
redirect('user/user_dash');
} {
$this->session->set_flashdata('flash_data', '<div class="alert alert-danger" role="alert" style="font-size:12px">
<center><b>Sorry !!</b> Username / Password is not correct.
</center>
</div>');
redirect('user/user_login');
}
For this to work, there must be a name column in the table tb_user
You need to store the full name in the session too.
Try editing this
$cek = $this->Model_user->cek_login('tb_user',$where)->num_rows();
if($cek > 0){
$data_session = array(
'username' => $username,
'status' => "userlogin"
);
to this
$cek = $this->Model_user->cek_login('tb_user',$where);
if($cek->num_rows() == 1){
$row=$cek->result_array()[0]; //returns an array of results, pick the first
$data_session = array(
'fullname' => $row['fullname'], //assuming column name is 'fullname'
'username' => $username,
'status' => "userlogin",
);
I'm having some issues with validating form fields inside my controller, for testing purposes.
I have my model Experience with hasMany ExperienceDetail. ExperienceDetail belongsTo Experience.
I created a form using the FormHelper which contains the following:
index.ctp
<?php
echo $this->Form->create('Experience', array('action' => 'index'));
echo $this->Form->input('Experience.date', array(
'label' => array(
'text' => 'Datum'),
'type' => 'date',
'dateFormat' => 'DMY',
'monthNames' => false,
'minYear' => date('Y') - 10,
'maxYear' => date('Y')
)
);
echo $this->Form->input('Experience.test');
echo $this->Form->input('ExperienceDetail.vertrekstation');
echo $this->Form->end('Verstuur!');
?>
There are some more fields provided with ExperienceDetail, but these are irrelevant for this matter.
Experience.php
<?php
class Experience extends AppModel {
public $name = 'Experience';
public $hasMany = 'ExperienceDetail';
public $validate = array(
'vertrekstation' => array(
'rule' => 'notEmpty',
'message' => 'Voer een vertrekstation in',
'required' => true
),
'test' => array(
'rule' => 'notEmpty',
'message' => 'Test mag niet leeg zijn!'
)
);
}
?>
ExperienceDetail.php
<?php
class ExperienceDetail extends AppModel {
public $name = 'ExperienceDetail';
public $belongsTo = 'Experience';
public $validate = array(
'vertrekstation' => array(
'rule' => 'notEmpty',
'message' => 'Voer een vertrekstation in'
)
);
}
?>
ExperiencesController.php
<?php
class ExperiencesController extends AppController {
public $helpers = array('Html', 'Form', 'Session');
public function index() {
// $this->layout = 'default_orig';
$this->set('title_for_layout', 'De OV-Ervaringenmeter!');
// LOAD Model Carrier
$this->loadModel('Carrier');
$this->set('carrier', $this->Carrier->find('list', array('order' => array('Carrier.name' => 'asc'))));
// Check if form is allready filled
if($this->request->is('post')) {
$this->Experience->set($this->request->data);
$this->Session->setFlash('No if or else statement is called');
if ($this->Experience->validates()) {
$this->Session->setFlash('Validates!');
}
}
}
}
?>
The problem is: when I send the form and leave test empty, it provides me the validation error which I've set up in the Model. But when I leave vertrekstation empty, it doesn't provide me any errors that belongs to the input field.
What am I doing wrong and how am I able to get these errors printed?
<? 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;
}
}