Codeigniter website taking ~30 seconds to load some pages - codeigniter

I have a codeigniter website that runs beautifully on localhost, but now that I have moved it to Host gator some page requests are taking around 30 seconds to serve up, if loaded at all! The weird thing is that is seemingly random, and while waiting for the page to load, if I simply re-click the link the page will load normally. I'm not sure if this is a programming problem in my controllers (code below) or just issues on host gators end. Please plesse please someone help me out here, as I am going insane.
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller
{
function __construct()
{
parent::__construct();
if (!$this->session->userdata('language')) $this->session- >set_userdata('language', 'en');
}
function index ()
{
$tags['title'] = 'title tag';
$this->load->view($this->session->userdata('language').'/includes/view_header',$tags);
$this->load->view($this->session->userdata('language').'/company/view_company');
$this->load->view($this->session->userdata('language').'/includes/view_footer');
}
function warranty ()
{
$tags['title'] = 'title tag';
$this->load->view($this->session->userdata('language').'/includes/view_header',$tags);
$this->load->view($this->session->userdata('language').'/company/view_warranty');
$this->load->view($this->session->userdata('language').'/includes/view_footer');
}
}

I suggest you to test with codeigniter profiler, It will show all the processing time like sql execution etc...
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Company extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->output->enable_profiler(TRUE);
if (!$this->session->userdata('language')) $this->session- >set_userdata('language', 'en');
}
When enabled a report will be generated and inserted at the bottom of your pages.
To disable the profiler you will use:
$this->output->enable_profiler(FALSE);
For more details http://codeigniter.com/user_guide/libraries/output.html
Hope this will help you, let us know if anything there... Thanks!!

Do you have any Javascript or external file on your site? That might be the problem.
Try using Firebug/YSlow and you'll probably get if an external js/file (Facebook or Twitter js for example) is taking lot of time to load.

Related

Unable to update session variable in CodeIgniter

I am in trouble using the CodeIgniter 2.2.6 session variables. I have a view, where some data is inputed by the user. Then, the user can press a submit button and this data goes to a web service, is processed and returned to the same view (using the controller and its model).
Bellow is the view code (v_index), where I save the data in the CodeIgniter session variables.
<?php
// loads the variables that will be used in print
$this->load->library('session');
$array_items = array(
'numberVehiclesUsed' => $numberVehiclesUsed,
'shortestRoute' => $shortestRoute,
'VRPSolution' => $VRPSolution,
);
$this->session->set_userdata($array_items);
?>
So, when I run the code bellow, I can see all the session data and everything is ok all the times. My application calls the web server, get the return and saves in the session variables everytime.
<?php
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
?>
So, I have another screen where I want to use the session data, called "Imprimir" (it means print). The controller code is bellow. The print_r is the for debuging proposes, offcourse.
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class C_imprimir extends CI_Controller
{
function __construct()
{
parent::__construct();
//load the session library
$this->load->library('session');
}
public function index()
{
$dados['numberVehiclesUsed'] = $this->session->userdata('numberVehiclesUsed');
$dados['shortestRoute'] = $this->session->userdata('shortestRoute');
$dados['VRPSolution'] = $this->session->userdata('VRPSolution');
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
$this->load->view('v_imprimir', $dados);
}
}
?>
When I run the code for the first time, it saves the data in the session variables and I am able to load in the controller "Imprimir" as the code above. The print_r shows that all the data is there and ok.
The problem is when I run the first view (v_index) again with new data, the session variables are updated but, the controller "Imprimir" loads only the first (and old) data, and never updates it.
I have no idea what I aḿ doing wrong. The others Stackoverflow questions about problems with CodeIgniter session variables did not help me.
Any suggestion?

php codeigniter uploading photos

So I was watching a video on how to upload photos using codeigniter. The link I used is here and the code is also at this site http://code.tutsplus.com/tutorials/codeigniter-from-scratch-file-uploading-and-image-manipulation--net-9452. I got everything to work however, when I tried to use the code on my own website I ran into a problem.
Basically all I changed was that I created a template for the view to be loaded in instead of just loading a single view. There is a controller Gallery.php file that looks like the follows.
<?php
class Gallery extends CI_Controller {
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$this->load->view('gallery_view',$data);
}
}
I simply changed this code to. Really only changing the last line and replacing it with those new three bottom lines.
<?php
class Gallery extends CI_Controller {
function index() {
$this->load->model('Gallery_model');
if ($this->input->post('upload')) {
$this->Gallery_model->do_upload();
}
$data['images'] = $this->Gallery_model->get_images();
$var = $this->load->view('gallery_view',$data,true);
$data['center_content'] = $var;
$this->load->view('includes_main/template',$data);
}
}
Now I get an strange error that I do not understand and no error shows up in the console log. Here is a picture of the error. http://i.imgur.com/tsKNj9W.png. The error says "unable to load the requested file" then doesn't have any file name after it. then there is a .php at the bottom of the page. I just don't have a clue what is giving me this error. I checked my template over again and again, but don't see anything wrong.
My template is as follows. I commented out everything just to make sure nothing else was giving me this error. So I am just left with one line.
<?php $this->load->view($center_content); ?>
Thanks for reading. Sorry I just have been stuck on this for a while and still haven't been able to fix it.
In this case there there are many mistakes. you did not load upload library and you also did not set config array. you can find on codeigniter user guide how to upload a file. I recommend to you read this link to upload a file in codeigniter
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

CodeIgniter - Including sections of code?

I have about 9 lines of code that will be the same in many of my controllers. They go in the index and are used to check whether the user is logged in, what level of access has been granted, and grab some session variables. What's the best way to include snippets of code that are not complete functions within themselves?
The snippets are not in all controllers, just the ones that build the admin section of the application.
It would be great if I could use one line of code to include the snippet but I'm new to CodeIgniter and I don't want to stray from best practices. If you could include a brief example that would help me visualize this. Thanks!
You want to create an extension of the base CI controller and do your checks there. Extending the cores are in the documentation here: http://ellislab.com/codeigniter/user_guide/general/creating_libraries.html Scroll down to the Extending Native Libraries part.
In your new controller you can do something like this:
<?PHP
class MY_Controller extends CI_Controller
{
function __construct()
{
parent::__construct(); //Gets all the CI_Controller functions and makes them available to this controller.
if($this->session->userdata('is_logged_in')
{
$this->load->model('categories');
$this->categories=$this->categories->getAllCategories();
}
}
}
Then in your other controllers you use MY_Controller instead of CI_Controller like this:
<?php
class Pages extends MY_Controller {
Obviously you'll have your own checks and variables to load but this should give you the general idea. In the example above I can now use $this->categories to retrieve my categories anywhere in the application that's been loaded with MY_Controller.
I'm using a 'main' template file (views/template.php):
// views/template.php
<?php $this->load->view('includes/header'); ?>
<?php $this->load->view('includes/' . $main_content); ?>
<?php $this->load->view('includes/sidebar'); ?>
<?php $this->load->view('includes/footer'); ?>
So the view directory structure is something like this:
--views
--includes
- header.php
- footer.php
- template.php
As you can see inside the view/includes/ folder I'm using some reuseable snippets like 'header.php', 'footer.php'...
So inside a controller I'm loading this main template file:
class MyController extends CI_Controller {
public function index()
{
//...
$this->load->view('template',$data);
}
}
So it loads 'template.php'.
And can use the $data inside header.php, footer.php too..
But you can vary this as you want, it's just a 'basic' idea..

How to implement a secured login/logout in Codeigniter?

I am making a secured login for the system I am creating with CI since there should be an admin control of the system.
My approach was that I made a file on the application/core directory named MY_System.php where I got this line of code:
class MY_System extends CI_Controller
{
function __construct()
{
parent::__construct();
}
}
Then I also have Admin_Controller.php where I got this code :
class Admin_Controller extends MY_System
{
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
function is_logged_in()
{
$is_logged_in = $this->session->userdata('is_logged_in');
if(!isset($is_logged_in) || $is_logged_in != true)
{
redirect(base_url(). 'login');
}
}
}
The 2 files above are saved inside the core folder of the framework.
While on the controllers directory of the system, I got several controllers let's say Person in instance where I have this code:
class Person extends Admin_Controller
{
function __construct()
{
parent::__construct();
}
}
I believe that in this way, whenever the are pages of the system that shall be restricted I would use the Admin_Controller as the parent of the classes so that the users can't directly access the pages.
Now, my problem is that as I would click logout, the session will be destroyed and the user's data are emptied thus, the user must be redirected to the login page but it doesn't go that way because whenever I would click the back button of the browser right after I click logout, the previous page where the user was would still show up but when I click the links on those page, that's the time when the user will be redirected to the login page. What I want is that, after clicking logout and if he/she attempts and clicks the back button of the browser, he/she must be redirected to the login page.
Does anyone know the solution for my problem? Any help is appreciated. Thanks in advance.
Try adding the follwing HTTP Header to your pages:
Cache-Control no-cache, no-store, must-revalidate
This will force your browser to redownload the page (= re-execute your controller which should check the is_logged_in variable and redirect to the login page)
Try doing this instead:
redirect(site_url('login'), 'refresh');

Multiple Web Pages in CodeIgniter

I am having trouble simply making multiple pages with CodeIgniter. For example, I am trying to make a simple About page with codeigniter. So I create an about.php controller and an about_view.php view file. However if I were to try and make a link from the home page to "http://miketrottman.com/about" it will go nowhere. I am sure I am fundamentally missing something but I have read and watched example videos I am just spinning my wheels on this project at this point. Here is my home.php controller, let me know if I should post any other code. My site is http://miketrottman.com. I am new to the CodeIgniter scene an any help is much appreciated!
home.php in Controller directory
'
class Home extends Controller {
function Home()
{
parent::Controller();
}
function index()
{
//Load Extensions
$this->load->database();
$this->load->library('session');
//Include these basics everytime the home page is loaded
$data['pageTitle'] = "Trottman's Blog";
$data['title'] = "Trottman's Blog Title";
$data['heading'] = "Trottman's Blog Heading";
//Load Proper CSS for users browser
$data['css'] = $this->getCSS();
//Load the Blog Model
$this->load->model('blog_model');
//Load the Blog Entries
$data['blog'] = $this->blog_model->getBlogEntries();
$data['blog_comments'] = $this->blog_model->getBlogCommentEntries();
//
//Load all of this information into the home_view Page
$this->load->view('home_view', $data);
}
function getCSS()
{
//Load user_agent library to pull user's browser information
$this->load->library('user_agent');
//Agent is now the users browser
$agent = $this->agent->browser();
//According to the users browser, this is what the CSS should be
if ($agent == 'Internet Explorer')
{
$cssFile = "ieStyle.css";
}
else
{
$cssFile = "style.css";
}
return $cssFile;
}
}?>
'
And I am dumb, my whole problem was I was trying to go to /about and what I should have been doing is http://miketrottman.com/index.php/about because I have yet to remove the index.php in my URIs.
So I guess, thanks Stack overflow for creating an outlet for my ignorance, perhaps others can learn from my mistake then!

Resources