Custom Profile URL Codeigniter - php

I'm trying to create the custom profile url like site/u/username
i found this one How to make url /username in codeigniter?
but until now it still show 404. when i'm trying to access site/u/username
my routes.php
$route['default_controller'] = "guest";
$route['404_override'] = 'u';
my model
function cekuser($username)
{
$query = $this->db->query("SELECT * FROM usertable WHERE username = '$username'");
$query = $query->result_array();
if($query){
return $query[0];
};
}
my controller
<?php class U extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index()
{
$username = $this->uri->segment(1);
if (empty($username)) {
$this->show_404();
}
$this->load->model('m_login');
// Check if parameter is not a valid username.
if (!$this->m_login->cekuser($username)) {
$this->displayPageNotFound();
} else {
$this->load->view('template');
}
}
protected function displayPageNotFound() {
$this->output->set_status_header('404');
$this->load->view('notfound');
}
}

route.php
instead of $route['404_override'] = 'u';
Add
$route['u/(:any)'] = 'u/showuserinfo';
In Controller
Add a method
function showuserinfo($name)
{
$this->load->model('m_login');
if (empty($name))
{
$name = $this->uri->segment(2);
}
$user = $this->m_login->cekuser($name);
if (!empty($user))
{
$this->load->view('template', $user);
}
else
{
echo 'User Not found';
}
}

$route['404_override'] = 'controller name';
Add
$route['controller name/(:any)'] = 'controller name/any name';

Related

How to redirect on controller?

I've created a PHP desktop application, but after login, I want to redirect to the controller, but I'm getting error:
redirect('dashboard');
Loading error (-102).
and when I like something this:
redirect('https://www.google.com/');
it works.
How can I redirect to a controller?
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('LoginModel','LoginModel');
}
public function index()
{
if(!empty($_POST))
{
$email = $this->input->post('email');
$password = $this->input->post('password');
$result = $this->LoginModel->login($email,$password);
if($result -> num_rows() > 0)
{
foreach ($result->result() as $row)
{
$this->session->userid = $row->id;
$this->session->email= $row->email;
$this->session->is_admin = $row->is_admin;
**redirect('dashboard');**
}
}
else
{
$data['email'] = $email;
$data['password'] = $password;
$this->session->set_flashdata('SUCCESSMSG','Email and Password is Wrong');
$this->load->view('login',$data);
}
}
else
{
$this->load->view('login');
}
}
public function logout()
{
$this->session->sess_destroy();
redirect('login');
}
}
I think you need to use redirect to relative path; try something like this:
redirect('/controller_name/Dashboard');

Can't load a specific page on codeigniter "slug"

I'm trying to load a specific page within a controller. I followed the Codeigniter tutorial and the main pages work but the individual page (loaded with view) doesn't load according to the given slug.
blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Blog extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/index
* - or -
* http://example.com/index.php/Index/index
* - or -
*/
function __construct()
{
parent::__construct();
$this->load->model('blog_model');
$this->load->helper('url_helper');
}
public function index()
{
$data['post'] = $this->blog_model->get_posts();
$data['title'] = 'Blog archive';
$this->load->view('header', $data);
$this->load->view('blog', $data);
$this->load->view('footer', $data);
}
public function view($slug = NULL)
{
$data['post'] = $this->blog_model->get_posts($slug);
if (empty($data['post']))
{
show_404();
}
$data['title'] = $data['post']['title'];
$this->load->view('header', $data);
$this->load->view('post', $data);
$this->load->view('footer', $data);
}
}
blog_model.php
<?php
class Blog_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_posts($slug = FALSE)
{
if ($slug === FALSE)
{
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
$query = $this->db->get();
return $query->result_array();
}
$this->db->select('*');
// $this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
// $this->db->where('slug', $slug);
$query = $this->db->get_where('blog_posts', array('slug' => $slug));
return $query->row_array();
}
}
As you can see I tried a few combinations because I'm not sure it's retrieving the table in get_posts when slug is not false.
try like this
call url_helper helper in __construct like following
$this->load->helper('url');
Now, update the routes.php like following
If your URL like
http://www.example.com/blog/view/slug
Your ruote should be like this
$route['blog/view/(:any)'] = 'blog/view/$1';
If your URL like
http://www.example.com/view/slug
Your ruote should be like this
$route['view/(:any)'] = 'blog/view/$1';
And, your get_posts model function repeating queries, use it simply like below
public function get_posts($slug = FALSE){
$this->db->select('*');
$this->db->from('blog_posts');
$this->db->join('category', 'category.id = blog_posts.category_id');
$this->db->join('author', 'author.id = blog_posts.author_id');
if($slug){
$this->db->where(compact('slug'));
}
$query = $this->db->get();
return ($query->num_rows() > 1) ? $query->result_array() : $query->row_array();
}
In the end I solved it by modifying the routes:
At first I had:
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/$1';
So I changed it to:
$route['default_controller'] = 'Index';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
$route['blog'] = 'blog';
$route['blog/(:any)'] = 'blog/view/$1';
And changed the method name in blog.php from blog to view. "view" looks more standard than "blog" so I left it that way. In 'blog/view/$1' represents the controller, view the method and of course $1 is the first param. Actually if I try Blog/view/hello-world it works too.

Session is not set after redirect in codeigniter

I've created a project in Codeigniter. My problem is when I log in, auth controller shows the value that is set in session $this->session->userdata("logged_in") but it is not redirecting to dashboard.
I also changed the PHP version on the live server from PHP 7.1 to PHP 5.6 but it's still not working. Session works perfectly on local server with xampp but not working on live server
Auth_model
public function Authentification() {
$notif = array();
$email = $this->input->post('email',TRUE);
$password = Utils::hash('sha1', $this->input->post('password'), AUTH_SALT);
$this->db->select('*');
$this->db->from('users');
$this->db->where('email', $email);
$this->db->where('password', $password);
$this->db->limit(1);
$query = $this->db->get();
if ($query->num_rows() == 1) {
$row = $query->row();
if ($row->is_active != 1) {
$notif['message'] = 'Your account is disabled !';
$notif['type'] = 'warning';
} else {
$sess_data = array(
'users_id' => $row->users_id,
'first_name' => $row->first_name,
'email' => $row->email
);
$this->session->set_userdata('logged_in', $sess_data);
}
} else {
$notif['message'] = 'Username or password incorrect !';
$notif['type'] = 'danger';
}
return $notif;
}
Auth controller
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
Utils::no_cache();
if ($this->session->userdata('logged_in')) {
redirect(base_url('dashboard'));
exit;
}
}
public function index() {
redirect(base_url('home'));
}
public function login() {
$data['title'] = 'Login';
$this->load->model('auth_model');
if (count($_POST)) {
$this->load->helper('security');
$this->form_validation->set_rules('email', 'Email address', 'trim|required|valid_email|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
if ($this->form_validation->run() == false) {
// $data['notif']['message'] = validation_errors();
// $data['notif']['type'] = 'danger';
$status = validation_errors();
if ( $this->input->is_ajax_request() ) {
echo json_encode($status);
exit;
}
}
else {
$data['notif'] = $this->auth_model->Authentification();
// it show the result here but not redirect to dashboard
// print_r($this->session->userdata("logged_in"));
// die("auth/login");
}
}
if ($this->session->userdata('logged_in')) {
redirect(base_url('dashboard'));
exit;
}
/*
* Load view
*/
$this->load->view('includes/header', $data);
$this->load->view('home/index');
$this->load->view('includes/footer');
}
dashboard
class Dashboard extends CI_Controller {
var $session_user;
function __construct() {
parent::__construct();
$this->load->model('auth_model');
$this->load->helper('tool_helper');
Utils::no_cache();
if (!$this->session->userdata('logged_in')) {
redirect(base_url('home'));
exit;
}
$this->session_user = $this->session->userdata('logged_in');
}
/*
*
*/
public function index() {
$data['title'] = 'Dashboard';
$data['session_user'] = $this->session_user;
// print_r($this->session->userdata("logged_in")); //its show empty
$data['items'] = $this->auth_model->get_all_products();
$this->load->view('includes/header', $data);
// $this->load->view('includes/navbar');
$this->load->view('includes/navbar_new');
$this->load->view('dashboard/index');
$this->load->view('includes/footer');
}
I don't know why session not set. I have been stuck in this for a week. Please help me out.
Try this.
Change $config['sess_save_path'] = sys_get_temp_dir(); to $config['sess_save_path'] = FCPATH . 'application/cache/sessions/'; in config.php

CodeIgniter - Unable to access an error message corresponding to your field name

I'm creating a library to add new users.
class Register
{
private $CI;
public function add_new_user()
{
$CI =& get_instance();
$CI->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
if ($CI->form_validation->run() == TRUE)
{
$email = $_POST['email'];
$insert_data = array('email' => $email);
$CI->new_data->add_user($insert_data);
}
}
private function is_email_exist($email)
{
$CI =& get_instance();
$email_result = '';
$query = $CI->check->find_email($email);
foreach ($query->result_array() as $row)
{
$email_result = $row['email'];
}
if ($email_result == $email)
{
$CI->form_validation->set_message('is_email_exist', 'Such email already exist!');
return FALSE;
}
else
{
return TRUE;
}
}
}
I add form_validation and models check, new_data to the autoload. When I submit a form instead of an error (if it should be there) I get Unable to access an error message corresponding to your field name Username.(is_username_exist). What should i do to get right error?
Codeigniter form validation is for the controller so when submit form it goes there
https://www.codeigniter.com/user_guide/libraries/form_validation.html#the-controller
Controller Way
<?php
class Register extends CI_Controller
{
public function __construct() {
parent::__construct();
$this->load->library('form_validation');
$this->load->model('some_model');
}
public function index() {
$this->form_validation->set_rules('email', 'Email', 'required|callback_is_email_exist');
if ($this->form_validation->run()) {
$insert_data = array(
'email' => $this->input->post('email')
);
$this->some_model->new_data->add_user($insert_data);
}
$data['title'] = 'Welcome To Codeigniter';
$this->load->view('header', $data);
$this->load->view('someview', $data);
$this->load->view('footer');
}
// Check user email
public function is_email_exist() {
// Single input check
$this->db->where('email', $this->input->post('email'));
$query = $this->db->get('user');
// If user email greater than 0
if ($query->num_rows() > 0) {
return TRUE;
} else {
$this->form_validation->set_message('is_email_exist', 'Such email already exist!');
return FALSE;
}
}
}
replace 'is_email_exist' to __FUNCTION__ work for me,
https://stackoverflow.com/a/38950446/2420302

codeigniter : user profile page

Hello i have problem with the user profile page in codeigniter .
So here is my user controller :
<?php
class User extends CI_Controller {
function users() {
parent::user();
}
function index($id = null) {
if($id == null) {
redirect('/', 'refresh');
}
else {
$data['title'] = 'User Page';
$data['result'] = $this->users_model->get_user_info();
$data['id'] = $id;
$data['main_content'] = "main/profile_view" ;
$this->load->view('home',$data);
}
}
}
?>
And this is the function in the model :
public function get_user_info(){
$this->db->where('id' , 'id');
$q = $this->db->get('users');
if ($q->num_rows > 0) {
return $q->result();
} else {
return false;
}
}
And this is the routes file :
$route['user/(:any)'] = "user/index/$1";
i get this error in localhost/cc/user/1
Fatal error: Call to a member function get_user_info() on null in C:\xampp\htdocs\cc\application\controllers\user.php on line 12
And i want to know how to display the user data in the view
in your controller i think this line:
$data['result'] = $this->users_model->get_user_info();
should be:
$data['result'] = $this->users_model->get_user_info($id);
and in your model this:
public function get_user_info(){
$this->db->where('id' , 'id');
should be this:
public function get_user_info($id){
$this->db->where('id' , $id);
// ================= next question
i tried it , but when i enter a non exist id after user/. , it displays the profile vue
yeah i was going to wrap it in an IF but then got lazy -- here's one way
check if the result did not come back -- so after you check to see if $id is NULL -- if $data['result'] did not come back from the model then go to a method like showUserNotFound($id).
elseif ( ! $data['result'] = $this->users_model->get_user_info($id) ){
$this->showUserNotFound($id); }
else { $data['title'] = 'User Page';
$data['id'] = $id;
$data['main_content'] = "main/profile_view" ;
$this->load->view('home',$data); }

Categories