Server Check Codeigniter - php

My redirect and server check not working for my index.php in codeigniter.
I have a front end install wizard for codeigniter with 4 steps the 4th step being the last. I am trying to make it so if user has gone through the steps and has reached step 4 that will have access to website on there domain/server.
Currently the code I use keeps on redirecting to the index.php of my install URL, even though I have completed all steps.
This code below is on my main index.php
if (!file_exists('install/index.php')) {
// Make sure we've not already tried this
if (strpos($_SERVER['REQUEST_URI'], '/install/')) {
header('Status: 404');
exit;
}
// Otherwise go to installer
header('Location: '.rtrim($_SERVER['REQUEST_URI'], '/').'/install/');
exit;
}
Welcome Controller
public function index()
{
if (!file_exists('install/index.php')) {
// Make sure we've not already tried this
if (strpos($_SERVER['REQUEST_URI'], '/install/')) {
header('Status: 404');
exit;
}
// Otherwise go to installer
header('Location: '.rtrim($_SERVER['REQUEST_URI'], '/').'/install/');
exit;
} else {
$this->load->view('welcome_message');
}
}

You can place this code in the welcome controller
Place your code inside the index function of the controller
class Welcome extends CI_Controller {
public function index()
{
/* your redirect code*/
}
public function comments()
{
echo 'Look at this!';
}
}

Related

PrestaShop: Redirect to homepage if the product didn't exists

i'm trying to redirect page like this :
https://www.cdsoft.co.il/index.php?id_product=303915&controller=product&id_lang=1
i want that every page, with deleted id_product - will be redirected to my homepage:
which means : i want to redirect "https://www.cdsoft.co.il/index.php?id_product=XXX"
how to do it in the htaccess file ?
I propose the #Labradorcode idea, but with the correct approach.
Make an ovverride of ProductController.php in rootofps/override/controllers/front/ProductController.php, so you have to create a new file in that directory (for rootofps I mean the install directory of PrestaShop), the content of that file is written below:
class ProductController extends ProductControllerCore {
public function init(){
FrontController::init();
if ($id_product = (int)Tools::getValue('id_product')) {
$this->product = new Product($id_product, true, $this->context->language->id, $this->context->shop->id);
}
if (!Validate::isLoadedObject($this->product)) {
header('HTTP/1.1 404 Not Found');
header('Status: 404 Not Found');
header('Location: '.$this->context->link->getPageLink('index', array('id_product', Tools::getValue('id_product'))));
} else {
parent::init();
}
}
}
In this case the redirect works if the product is totally removed and not if is disabled.
After you have created the file delete this file rootofps/cache/class_index.php
Maybe this will be helpful.
Edit file controllers/front/ProductController.php, line 91 or find Tools::displayError('Product not found'), and change this for this:
header("Location: /");

Redirect Class not working on shared hosting

I was following OOP Login System, php video tutorials from Codecourse. In the video series they have taught to create a Redirect class (Redirect.php):
<?php
class Redirect {
public static function to($location = null) {
if($location) {
if(is_numeric($location)) {
switch($location) {
case 404:
header('HTTP/1.0 404 Not Found');
include 'includes/errors/404.php';
break;
}
}
header('Location: '. $location);
exit();
}
}
}
Now I'm using the above class in a php file as
<?php
require_once 'Redirect.php';
Redirect::to('index.php');
?>
The code works fine on localhost (WAMP Server) but it's not working on godaddy hosting. How to get rid of this problem ? Do I need to make any changes in Redirect class ?
Please help.

codeigniter: base_url part won't redirect to my controller

I am trying to make a log in work. I am all good with the verification but is stuck when it redirects to an another controller to show the view of the logged in page. I am still new to codeigniter and is still not sure about how controllers work.
This is my controller for verifying logged in users:
function index() {
$this->form_validation->set_rules('studentid', 'studentid', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
if($this->form_validation->run() == FALSE) {
$this->load->view('v_login');
} else {
//Go to private area
redirect(base_url('c_home'), 'refresh');
}
}
Its function is to validates the user if its in the database, however when the user is successfully logged in, it won't redirect to this redirect(base_url('c_home'), 'refresh'); It tells me that
Object not found!
The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.
If you think this is a server error, please contact the webmaster.
This is the c_home.php where it is supposed to be redirected:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_home extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('m_login','',TRUE);
$this->load->helper('url');
$this->load->library(array('form_validation','session'));
}
function index() {
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$data['studentid'] = $session_data['studentid'];
$this->load->view('v_home', $data);
} else {
//If no session, redirect to login page
redirect('c_login', 'refresh');
}
}
function logout() {
//remove all session data
$this->session->unset_userdata('logged_in');
$this->session->sess_destroy();
redirect(base_url('c_login'), 'refresh');
}
}
is it okay to redirect a controller from another controller?
after it redicts to c_home.php
it will show the v_home.php
<!DOCTYPE html>
<head>
<title>Simple Login with CodeIgniter - Private Area</title>
</head>
<body>
<h1>Home</h1>
<h2>Welcome <?php echo $studentid; ?>!</h2>
Logout
</body>
</html>
You don't need the base_url() in the redirect. just use
redirect('c_home',refresh);
Although there are 2 things i should make you aware of, you need ('controller/function') not just ('controller'). And also make sure you're loading $this->load->helper('url') in your __construct function on both controllers.
Although just for future reference, i think this is what you meant.
redirect(base_url().'c_home',refresh)
Codeigniter's redirect function already has the site_url() embedded.
So, instead of this;
redirect(base_url('c_login'), 'refresh');
Use this, as you have earlier in your code
redirect('c_login', 'refresh');
Hope this helps

codeigniter - controller load multiple views

I made a website but in two versions one for normal users and one for the mobile users and for both I made view page and also with multilanguage options, first I add in controller
public function index()
{
if ($this->input->get("lang") =="en")
$this->load->view('en_signup');
else
$this->load->view('ar_signup');
$this->load->helper('url');
}
}
I made pages with name of marabic.php and menglish.php for mobile users now first I need to load these pages also but not mix with the original/default view pages, because I already mention java cript in default view page when its detect mobile user it redirect to m.domainname.com now I want to figure out this issue, please suggest.
Try this:
public function index()
{
$this->load->library('user_agent');
$this->load->helper('url');
if ($this->input->get("lang") =="en"){
if ($this->agent->is_mobile()) {
$this->load->view('menglish');
} else {
$this->load->view('en_signup');
}
} else {
if ($this->agent->is_mobile()) {
$this->load->view('marabic');
} else {
$this->load->view('ar_signup');
}
}
}
You can detect if a user is visiting from a mobile device by using CodeIgniter's User Agent library.
$this->load->library('user_agent');
if ($this->agent->is_mobile()) {
// Load mobile view
}

php host no redirecting properly on LAN

i have this piece of code, saved inside a file called 'functions.php'
$host = $_SERVER['HTTP_HOST'];
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
function redirect_to_home(){ /*function to redirect the user ot his profile page, or admin page if he is an administrator*/
if(admin_class::isadmin()){
header("Location:http://$host$folder//adminindex.php");
exit();
}
else{
header("Location:http://$host$folder/profile.php");
exit();
}
}
function redirect_to_welcome(){
header("Location:http://$host$folder/index.php");
exit;
}
function redirect_to_loan(){
header("Location:http://$host$folder/loan.php");
exit;
}
When browsing through the website itself, these don't work correctly and I could only navigate via links within the website, I've finished the whole thing though (I just used header("Location:http://localhost/YMMLS//pagename.php") when I was developing).
And I need some enlightenment here, I'm launching this website via LAN and those connected could access it via xxx.xxx.xxx.x/YMMLS. But of course, the website fails to redirect properly when any of these functions are called.
Thanks in advance,
NetBeans is warning you, because the variables are declared outside the function.
They don't exist within the scope of the function. Sure, you can call the function -- from outside the function, where those variables do exist -- but that's different. :)
Here -- try this;
class redirect {
public $host = '';
public $folder = '';
public function __construct() {
$this->host = $_SERVER['HTTP_HOST'];
$this->folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
}
public function home() { /*function to redirect the user ot his profile page, or admin page if he is an administrator*/
if(admin_class::isadmin()){
header("Location: http://$host$folder/adminindex.php");
} else {
header("Location: http://$host$folder/profile.php");
}
exit();
}
public function welcome() {
header("Location: http://$host$folder/index.php");
exit;
}
public function loan() {
header("Location: http://$host$folder/loan.php");
exit;
}
}
Instead of calling simply redirect_to_home(); this would be invoked by redirect::home();.
Hope that helps.
try this
host = $_SERVER['HTTP_HOST'];
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
function redirect_to_home(){
global $host, $folder;
if(admin_class::isadmin()){
header("Location:http://$host$folder//adminindex.php");
exit();
}
else{
header("Location:http://$host$folder/profile.php");
exit();
}
}
If this works then change this in all other functions
GOOD LUCK!!
DOCUMENTATION http://php.net/manual/en/language.variables.scope.php

Categories