page redirect issue codeigniter - php

I am unable to redirect to page using the redirect() in codeigniter. Also the same problem if I try to use location.href within view. It just redirects me to the page without the css and js includes
mY CONFIG
$config['base_url'] = 'http://localhost/tsb/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI'; //I have switched the 3 protocols too
HTACCESS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
BOOK CONTROLLER
public function psg_sel()
{
$book_name = $this->input->post('book_name');
$book_id = $this->cj_model->get_book_id($book_name);
$ch_id = $this->input->post('chapter_id');
$cj_mask = $this->input->post('cj_mask');
if($cj_mask == ''){
$psg_sel = $this->cj_model->psg_sel($book_id, $ch_id);
if($psg_sel === true){
redirect(base_url() . 'passage/', 'refresh');
}
}
}
PASSAGE CONTROLLER
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Passage extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->database();
}
public function index()
{
$data['page_name'] = 'passage';
$this->load->view('pages/index', $data);
}
}
Please help I dont know whats going wrong. I can access http://localhost/tsb/passage/ directly from address bar but none of these will work properly location.href="passage/";or redirect(base_url() . 'passage/', 'refresh'); This is how it displays

At controller try this code. First load url helper then try..
The redirect statement in code igniter sends the user to the specified web page using a redirect header statement.This statement resides in the URL helper which is loaded in the following way:
$this->load->helper('url'); //loads url helper
Controller:
public function psg_sel()
{
$this->load->helper('url'); //loads url helper
$book_name = $this->input->post('book_name');
$book_id = $this->cj_model->get_book_id($book_name);
$ch_id = $this->input->post('chapter_id');
$cj_mask = $this->input->post('cj_mask');
if($cj_mask == ''){
$psg_sel = $this->cj_model->psg_sel($book_id, $ch_id);
if($psg_sel === true){
redirect(base_url('passage'), 'refresh');
}
}
}

Related

Codeigniter 3 index method behaving unexpectedly

I have a CodeIgniter project on a subdomain. Now, When I visit sub.example.com it loads a login page and on successful login, it redirects to dashboard. Once logged in and session are in place visiting sub.example.com/login/ will auto-redirect to the dashboard page. Now here's my problem. After successful login, visiting sub.example.com doesn't redirect anywhere it simply load the login page. But visiting sub.example.com/index.php does redirect me to the dashboard page.
For some reason, my index method is called or working properly.
Here my code.
.htaccess
IndexIgnore *
php_value date.timezone Asia/Kolkata
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
## Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://sub.example.com/$1 [R=301,L]
## Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^sub.example.com$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
config.php
if ($_SERVER['REMOTE_ADDR'] == "127.0.0.1") {
$config['base_url'] = "http://" . $_SERVER['SERVER_NAME'];
} else {
$config['base_url'] = "https://" . $_SERVER['SERVER_NAME'];
}
routes.php
$route['default_controller'] = 'root';
$route['root_controller'] = 'root';
/*API CONTROLLER*/
$route['api_controller'] = 'api';
/*Guest Controller*/
$route['guest_controller'] = 'guest';
/*Custom Routes*/
$route['dashboard/(:any)'] = 'root/dashboard/$1';
$route['search/(:any)'] = 'root/search/$1';
$route['search/(:any)/(:num)'] = 'root/search/$1/$2';
$route['export/(:any)'] = 'root/export/$1';
root controller
public function __construct()
{
parent::__construct();
$this->default_data = array("project_name" => PROJECT_NAME);
if ($this->router->method == "privacy_policy") {
return;
}
$this->load->library('session');
if ($this->router->method == "login") {
if ($this->session->userdata("username")) {
redirect(base_url("dashboard/"));
}
} else {
if (!$this->session->userdata("username")) {
redirect(base_url("login/"));
}
}
//Set MYSQL timezone
$this->db->query("SET time_zone = '+05:30'");
}
/**
* Dashboard View
*/
public function index()
{
redirect(base_url("/dashboard/"));
}
/**
* Login View
*/
public function login()
{
$data = $this->default_data;
if ($_POST) {
$username = $this->input->post("username");
$plain_password = $this->input->post("password");
$this->load->model("authenticate");
if (!$this->authenticate->auth($username, $plain_password)) {
$data['message'] = "Invalid Credentials";
}
}
$this->load->view('login', $data);
}
Update
Forgot to mention that I am having this issue only on the remote server. On localhost it's working fine.
Hers is an idea - add this after you have loaded the session object:
if ($this->session->userdata("username") && $this->router->method == "index") {
redirect(base_url("dashboard/"));
}
Please use this .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
if you visiting sub.example.com/index.php successfully then try this
public function login()
{
$data = $this->default_data;
if ($_POST) {
$username = $this->input->post("username");
$plain_password = $this->input->post("password");
$this->load->model("authenticate");
if (!$this->authenticate->auth($username, $plain_password)) {
$data['message'] = "Invalid Credentials";
}else{
//go to dashboard function
$this->index();
}
}
$this->load->view('login', $data);
}
Turns out there was some issue with files on the remote server. I don't exactly know if the files were different or corrupted but replacing the whole project on the remote server fixed the issue
"Turns out there was some issue with files on the remote server. I don't exactly know if the files were different or corrupted but replacing the whole project on the remote server fixed the issue" --
#Akash You should compare your local and remote .htaccess files. I think that will explain your problem.

Error 404 when routing

I'm trying to do my site like this tutorial http://www.codeigniter.com/user_guide/tutorial/static_pages.html
but have some problem with routing. I have page "createBook" for default and when I call localhost it's work! But when I do like localhost/createBook I have Error 404. What I'm doing wrong?
In my controller:
public function view($page = 'createBook')
{
if ( ! file_exists(APPPATH.'views/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$this->load->view($page);
}
routes.php file
$route['default_controller'] = 'Books/view';
$route['(:any)'] = 'Books/view/$1';
And I have view files in my views folder named Success and createBook
I don't really get your question but it seems to me that you are trying to call a controller named createBook from the url, but in your function it shows that if there is no views/createBook.php than show 404, maybe its why you get a 404, because your calling a controller as a view, i think your function should be like this :
public function view($page = 'createBook')
{
if ( ! file_exists(APPPATH.'controllers/' . $page . '.php'))
{
// Whoops, we don't have a page for that!
$this->output->set_status_header('404');
show_404();
}
$this->load->view($page);
}
If you want your url to look something like this :
example.com/view/book/3
Your controller should look like this :
class View extends CI_Controller
{
public function book($book_id)
{
//Search the database for records about a book with its id, and return the data
//and assign it to a variable(in this case $data) ex : $data["book-name"] = ...
//And on your view you can call these values using ex : echo $book-name
$this->load->view('books_view', $data);
}
}
And make sure your .htaccess file next to the index.php looks like something like this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Then go to your config, and change this line to :
$config['index_page'] = '';
Now you can access all of your controllers this way :
http://www.example.com/controller_name

PHP MVC url Not Found 404

I am getting a URL not found error in my MVC Php application. The .htacess file seems fine and apache is configured well because the other application runs well. Am hosting my mysql db on amazon.
here is my code snippet.
LoginForm.php
/**
*
*/
class LoginForm extends Controller {
public $model;
public function index() {
//check if they are already logged in
if (!isset($_SESSION['email'])) {
require 'application/views/login/index.php';
} else {
//redirect to admin data
header("Location:" . URL . "home");
}
}
public function login() {
// get the post
$this->model = $this->loadModel('login');
if (isset($_POST['email']) && isset($_POST['password']) && isset($_POST['country'])) {
// echo "priv_".$_POST["country"];
$validate = $this->model->validate($_POST['email'], MD5($_POST['password']), $_POST["country"], 1);
if ($validate != 0) {
// get all the data
$data = $this->model->getByID($_POST['email']);
/*
echo "<pre>";
var_dump($data);
echo "</pre>";
exit();
*/
// set the session
session_start();
$_SESSION['email'] = $_POST['email'];
/*
* Privilege Sesssion settings Start
*
*/
$_SESSION["pnya"] = $data[0]['pnya'];
/*
* Privilege Session End
*/
header("Location:" . URL . "home");
} else {
header("Location:" . URL . "LoginForm");
}
} else {
// #todo reload login page page
header("Location:" . URL . "LoginForm");
// #todo wth appropriate errors
}
// use php to check if its an email
// if not set the errors
// #todo use model to get dta a from staff and validate
// #todo if it all succeeds then rdirect
}
public function logout() {
// destroy the session
session_start();
session_destroy();
// redirect to login page
header("Location:" . URL . "LoginForm");
}
}
?>
.htacess
Options -MultiViews
RewriteEngine On
Options -Indexes
RewriteBase /MIS/ysw/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
config.php
error_reporting(E_ALL);
ini_set("display_errors", 1);
define('URL', 'http://42.11.223.45/MIS/ysw/');
define('DB_TYPE', 'mysql');
define('DB_HOST', 'XXX');
define('DB_NAME', 'amazon');
define('DB_USER', 'xxxx');
define('DB_PASS', 'ddddd');
I have spent close to 13hrs trying to figure out what I have not done right, but will be glad if informed
Please check if mod_rewrite is working, you can take help from here: https://docs.bolt.cm/3.0/howto/making-sure-htaccess-works

Unable to redirect to a new page - CodeIgniter

I have a login system that works, but on the redirect function it gives me the error The requested URL "http://localhost/musiclear/index.php/welcome" cannot be found or is not available. Please check the spelling or try again later.
This is where I am using it (login.php):
function validate_credentials() {
$this->load->model('membership_model');
$query = $this->membership_model->validate();
if ($query) { // if users credentials validated
$data = array('usernames' => $this->input->post('username'),
'is_logged_in' => true);
$this->session->set_userdata($data); //set session data
redirect('welcome', 'refresh'); //redirect to home page
} else { //incorrect username or password
$this->index();
}
}
This is where I am directing it to (welcome.php):
class Welcome extends CI_Controller {
public function index() {
$this->home();
}
public function home() {
$this->load->model('model_users');
$data['title'] = 'MVC Cool Title'; // $title
$data['page_header'] = 'Intro to MVC Design';
$data['firstnames'] = $this->model_users->getFirstNames();
// just stored the array of objects into $data['firstnames] it will be accessible in the views as $firstnames
$data['users'] = $this->model_users->getUsers();
$this->load->view('home_view', $data);
}
}
Im thinking it is something wrong with the path, or where its linking to but im not sure. This is my directory setup:
Can someone please tell me whats wrong and how I can make it link to my page? Thanks so much
Have you created the .htaccess in your application folder?
Maybe this can work for your project:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /musiclear/index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /musiclear/index.php
</IfModule>
You are welcome

How to run function of another controller with base_url specified in config.php

I have controller welcome below that redirect to function of another controller in controllers/auth/login.php
function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('tank_auth');
}
function index() {
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login');
} else {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$this->load->view('welcome', $data);
}
}
Here, the config.php:
$config['base_url'] = '';
$config['index_page'] = 'index.php';
it work well. But when i specified the base_url in config file into:
$config['base_url'] = 'http://localhost/cilog/';
$config['index_page'] = '';
Object not found. why it be? but it work again when i specified index_page into index.php.
I believe this is because of the way CodeIgniter handles it's URL's. I'm not actually sure why Codeigniter does this, but they include index.php in there URL.
So your URL would look something like http://localhost/cilog/index.php/auth/login
You can either rewrite your .htaccess file to remove the index.php by putting this in:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
and keep your $config['base_url'] set too "http://localhost/cilog/"
OR
specify both a $config['base_url'] and $config['index_page']
See here for more info: http://ellislab.com/codeigniter/user-guide/general/urls.html (Removing the index.php file)

Categories