codeigniter URL change - php

I am creating eCommerce in Codeigniter. Here is the controller file for registration:
class Register extends CI_Controller {
public function index()
{
$this->load->view('common/header');
$this->load->view('register');
$this->load->view('common/footer');
}
public function home(){
$this->load->view('common/header');
$this->load->view('common/slider');
$this->load->view('home');
$this->load->view('common/footer');
}
public function signup(){
$username = $this->input->post("username");
$password = $this->input->post("password");
$email = $this->input->post("email");
$this->form_validation->set_rules('username','User Name','required|is_unique[user.name]|min_length[4]');
$this->form_validation->set_rules('password','Password','required|min_length[5]');
$this->form_validation->set_rules('email','Email address','required|valid_email');
$this->form_validation->set_error_delimiters('<div class="text-danger">', '</div>');
if($this->form_validation->run() == true){
$this->load->model('register_model');
if($this->register_model->register_user($username,$password,$email)){
$this->home();
}else{
$this->index();
}
}else{
$this->index();
}
}
}
On form submit the page is redirecting to the home page but the URL on the browser is (http://localhost:8080/codignitor_projects/shophere/register/signup) But I want to change the URL to this (http://localhost:8080/codignitor_projects/shophere/)

I got your problem. I don't know your CI version. I hope you are using the latest one and I am also giving my answer according to the latest version CI-4.
Please Write
return redirect()->to('index');
or
return $this->response->redirect(base_url('YourControllerName/index'));
instead of
$this->index();
To clearify this problem I would like to give you an example. Let, I have a Test controller and there are two functions such as abc() and def()
class Test extends Controller
{
function abc()
{
echo "Hi ! I am ABC";
}
function def()
{
$this->abc();//URL will be: http://localhost/your_project_name/public/test/def
//return redirect()->to('abc'); //URL will be: http://localhost/your_project_name/public/test/abc
//return $this->response->redirect(base_url('Test/abc')); //URL will be:http://localhost/your_project_name/public/Test/abc
}
}
There are three lines inside the def() function that give you the same result but the URL from first one to others will be different which is commented.

Related

codeigniter validation process for checking same data in one table

$sort_order=$this->input->post('sort_order','Sort Of Category','trim|required|max_length[128]|xss_clean|is_unique[categories.sort_order]');
Why is the is_unique function not working here?
It should be for set_rules on your form validation on controller
https://www.codeigniter.com/user_guide/libraries/form_validation.html
$this->form_validation->set_rules('sort_order','Sort Of Category','trim|required|max_length[128]|xss_clean|is_unique[categories.sort_order]');
https://www.codeigniter.com/user_guide/libraries/form_validation.html#setting-validation-rules
https://www.codeigniter.com/user_guide/libraries/form_validation.html#rule-reference
<?php
class Example extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
}
public function index() {
$data['title'] = 'Example';
$this->form_validation->set_rules('sort_order','Sort Of Category','trim|required|max_length[128]|is_unique[categories.sort_order]');
if ($this->form_validation->run() == false) {
$this->load->view('header', $data);
$this->load->view('example', $data);
$this->load->view('footer');
} else {
// You can put your update or insert model function here
redirect('success_controller');
}
}
}
$c_name=$this->input->post('c_name');
$sort_order=$this->input->post('sort_order');
$this->load->library('form_validation');
$this->form_validation->set_rules('c_name','Category Name','trim|required|max_length[128]|xss_clean');
$this->form_validation->set_rules('s_order','Sort Order','Sort Of Category','trim|required|xss_clean|is_unique[categories.sort_order]');

Unable to load view error in codeigniter 3

Controller:
class Welcome extends CI_Controller {
public function __construct()
{
parent::__construct();
if(!is_logged_in()){
redirect('admin');
}
}
public function index()
{
$this->load->view('templates\sub_header');
$this->load->view('welcome_message');
$this->load->view('templates\footer');
}
}
when called controller should simply load three views in sequence. Instead I keep getting error "Unable to load the requested file: templates\sub_header.php".
Important point to note is that this code is working perfectly fine on localhost but giving issues on godaddy subdomain.
I have defined a base url in the config file which seems to work fine.
On other hand another controller is working just fine everywhere:
public function index()
{
if( is_logged_in() )
{
redirect('welcome');
}
$this->form_validation->set_error_delimiters('<div class="text-danger">', '</div>');
$this->form_validation->set_rules('username', 'UserName', 'required');
$this->form_validation->set_rules('password', 'Password', 'required|min_length[4]');
$this->load->view('templates/header');
if($this->form_validation->run() == FALSE)
{
$this->load->view('auth/login_view');
}
else
{
// Check the entered value against db
$this->load->model('auth/admin_model');
$result = $this
->admin_model
->verify_user($this->input->post('username'),$this->input->post('password'));
if($result != false)
{
$username = $this->input->post('username');
$status = $result->status;
if($status == 1)
{
$user_roles = $this->admin_model->get_user_access_roles($result->id);
$this->session->set_userdata('username', $username);
$this->session->set_userdata('user-management',$user_roles->user_management);
$this->session->set_userdata('client-information',$user_roles->client_information);
$this->session->set_userdata('master-metadata',$user_roles->master_metadata);
$this->session->set_userdata('reports',$user_roles->reports);
redirect('welcome');
}
else
{
$data['error_message'] = "Account is disabled. Get in touch with system administrator !";
$this->load->view('auth/login_view',$data);
}
}
else
{
$data['error_message'] = "Invalid UserName or Password. Try Again !";
$this->load->view('auth/login_view',$data);
}
}
$this->load->view('templates/footer');
}
Views are loaded perfectly in Admin controller but after login when admin controller redirects to welcome controller, views stop loading. Only difference between the controllers is that in Admin controller i am calling header while in welcome i am calling sub_header but both file are present inside views folder.
Change \ to this /
public function index()
{
$this->load->view('templates/sub_header'); # Changed
$this->load->view('welcome_message');
$this->load->view('templates/footer'); # Changed
}
and make sure Files are exist

How to check whether or not the user is n admin instead of a normal user for all my admin functions?

First off, heres my code:
<?php
class Admin extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->model('admin_model');
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('home', 'refresh');
}
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
if ($session_data['id'] != 1){
$this->load->view('head');
echo "Sorry, you have to be an administrator to access this page";
$this->load->view('footer');
} else {
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('head');
$this->load->model('admin_model');
$data['users'] = $this->admin_model->show_data();
$data['bikes'] = $this->admin_model->show_bikes();
$this->load->view('admin', $data);
$this->load->view('footer');
}
} else
{
redirect('login', 'refresh');
}
}
The dilemma I'm in is, yes this will stop all normal users accessing the admin/index page. However they still will be able to access admin/create, admin/update and so on unless i put an if statement on all the functions which will take some time. Is there a quicker way (possibly something i could put in the construct) that will apply the check to all the admin pages? Thanks
Simply move your admin-check code to the constructor:
<?php
class Admin extends CI_Controller {
public function __construct()
{
parent::__construct();
if(!$this->session->userdata('logged_in')) {
redirect('login', 'refresh');
}
if ($session_data['id'] != 1){
$this->load->view('head');
echo "Sorry, you have to be an administrator to access this page";
$this->load->view('footer');
exit; // or return? sorry, not too familiar w/ CodeIgniter
}
$this->load->model('admin_model');
}
...
public function index()
{
...
}

Displaying Database

I have the following.
controllers/customers.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class customers extends CI_Controller {
function __construct()
{
parent::__construct();
}
public function view($id) {
$this->load->model('customers');
$news = $this->customers->view_customer($id);
$data['title'] = $news['title'];
$data['body'] = $news['body'];
$this->load->view('customers_customer_view', $data);
}
function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['username'] = $session_data['username'];
$this->load->view('customers_view', $data);
}
else
{
//If no session, redirect to login page
redirect('login', 'refresh');
}
}
function logout()
{
$this->session->unset_userdata('logged_in');
session_destroy();
redirect('dashboard', 'refresh');
}
}
?>
models/customer.php
<?php
class customers_model extends CI_Model {
public function __construct() {
$this->load->database();
}
public function view_customer($id) {
if($id != FALSE) {
$query = $this->db->get_where('news', array('id' => $id));
return $query->row_array();
}
else {
return FALSE;
}
}
}
?>
views/customers_customer_view.php
<?php print $title; ?>
<?php print $body; ?>
I am very new to code igniter, I have followed this tutorial from the web, No matter what i do i cannot get the database info to display when loading root/customers/view/1
All i get is a blank page. Even if i change the view to include a some static text it wont display, From this i believe it to be something wrong with loading the view, But all looks ok to me.
Please can somebody assist.
You wrote:
$this->load->model('customers');
But model file is named: customer.php.
And class name is: customers_model.
Please check it again.
I will give you an example:
$this->load->model('customers');
Your model file have to be: customers.php.
And your class name have to be: class Customers {}

Codeigniter 2.1 - Call function in __construct

I am having following functions:
public function admin_login_check()
{
if($this->session->userdata('is_logged_in') == FALSE)
redirect('admin/admin_login');
}
and this one (for the moment):
public function index()
{
$this->admin_login_check();
$data['title'] = 'Početna | TOP';
$data['main_content'] = 'admin_home';
$data= $this->data + $data;
$this->load->view('admin/admin_template', $data);
}
public function admin_login()
{
$data['title'] = 'Prijava | TOP';
$data['main_content'] = 'admin_login';
$data= $this->data + $data;
$this->load->view('admin/admin_template', $data);
}
When I $this->admin_login_check() inside __construct function I get following error:
The web page at xxx/admin/admin_login has resulted in too many redirects.
The reason you are having too many redirects is because when you place
$this->admin_login_check();
inside your admin's constructor controller. This will always run on each method/function inside your admin controller.
And since you redirected to the same admin controller,
redirect('admin/admin_login');
It will check first if you're logged out, so it will try to redirect you again and again.
One solution is to actually separate your admin_login page on a separate controller. Maybe the home controller if you have that.
You can make one check for current method name as:
public function admin_login_check()
{
$loged_in = $this->session->userdata('is_logged_in') ? TRUE : FALSE;
if($this->router->method != 'admin_login' && $loged_in==FALSE)
redirect('admin/admin_login');
}
The IF checks if you are at any page except the admin_login page and you are not logged in.
How ever I think this method for redirecting is little wrong.
It will be a lot better if you make one function is_admin to return TRUE/FALSE and do something like this...
public function __construct()
{
parent::__construct();
if($this->is_admin() == FALSE && $this->router->method != 'admin_login')
redirect('admin/admin_login');
}
public function is_admin()
{
return $this->session->userdata('is_logged_in') ? TRUE : FALSE;
}

Categories