6

I downloaded a website from ftp . The website is made with CodeIgniter. My problem is it doesn't seem to work on localhost.

Base URL in config.php:

$config['base_url'] = 'http://sample_localhost/mysite/';

Settings in database.php:

$db['default']['hostname'] = 'sample_localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'admin';
$db['default']['database'] = 'my_database';

Any help is very much appreciated. Thanks in advance!

tara giska
  • 101
  • 1
  • 2
  • 7

5 Answers5

9

1) Download the latest version of CodeIgniter.

2) Extract it and paste the extracted folder at the ‘htcdocs’ directory. In my scenario, I am using XAMPP 1.8.1, so I will paste it on the same directory. Also, you can rename the folder E.g. CI.

enter image description here

3) Take a look first at your config files and made some few modifications.

autoload.php

$autoload['libraries'] = array('database');
$autoload['helper'] = array('url');

config.php

$config['base_url'] = 'your localhost url';

in my case:

$config['base_url'] = 'http://localhost/CI/index.php/'; // your current URL on the address bar when displaying the welcome_message
$config['index_page'] = 'index.php'; // page where you want your viewers are redirected when they type in your website name

E.g. base_urlhttp://www.example.com/ index_page — index.php or straight ahead to news.php, it’s up to you

routes.php

$route['default_controller'] = 'site' // your controller's method, originally "welcome" to display welcome message

I set “site” as the default controller

database.php

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = '[your database]'; // e.g. CI_series
$db['default']['dbdriver'] = 'mysql';

TIP: Username as a default would be root if you don’t have any permissions for accessing the database yet. Also, leave password blank for now.

4) Start working with the Controllers Controllers are the heart of your application, as they determine how HTTP requests should be handled. A Controller is simply a class file that is named in a way that can be associated with a URI.

E.g.

http://www.example.com/index.php/blog/

In the above example, CodeIgniter would attempt to find a controller named blog.php and load it.

When a controller’s name matches the first segment of a URI, it will be loaded.

Reference

Now, let’s type the code for our controller.

<?php
class Site extends CI_Controller
{
    function index()
    {
        $this->load->view('home.php');
    }
}
?>

Basically, this will just load our view/page called home

* What is load?

Loader, as the name suggests, is used to load elements. These elements can be libraries (classes) View files, Helpers, Models, or your own files. (ref)

This code snippet would let you display the page, home.php. Also since, you’re calling home.php, you must have this page under the views folder. Create your home.php, write anything you would like to display as a test for our first run and save it.

This code snippet would let you display the page, home.php. Also since, you’re calling home.php, you must have this page under the views folder. Create your home.php, write anything you would like to display as a test for our first run and save it.

home.php

<p>
My view has been loaded. Welcome!
</p>

Also, save our controller under the controllers folder, file name should be the same as your class name. In this case, it should be saved as site.php.

The first run:

enter image description here

GIPSSTAR
  • 2,050
  • 1
  • 25
  • 20
5

My be it should be like

$config['base_url'] = 'http://localhost/mysite/';

or like

$config['base_url'] = 'http://MY_IP/mysite/';

And check your index value in config file like

$config['index_page'] = 'index.php';

if it is so then you need to add index.php in your url before the controller name and check your folder has permissions also...and at your router.php see this

$route['default_controller'] = "welcome";

you can change if you want to display a controller by defaulty when you call sample_localhost/my_site

GautamD31
  • 28,552
  • 10
  • 64
  • 85
  • actually its localhost, i just changed it to sample_localhost here in stackoverflow as it doesn't allow the "localhost" word in questions :) – tara giska Jun 07 '13 at 09:33
  • Still the same, doesn't work either. By the way here is my htaccess code: RewriteEngine on RewriteCond $1 !^(index\.php|images|css|js|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L] – tara giska Jun 07 '13 at 09:39
2

Replace base url with following code will run & get speed

$config['base_url'] = '';
$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);
Nitin Karale
  • 789
  • 3
  • 12
  • 34
0

The solution I've implemented, works with different scenarios that have problems on local host: HTTP 404 error, Windows 10 [::1] issue, Session not working on localhost issue, session class not working in Chrome issue.

In local host environment:

1. Do not use .htaccess, remove or rename the file.

2. In config.php, make explicit the base url and the index page.

// Base Site URL
$config['base_url'] = 'http://localhost/';

// Index File
$config['index_page'] = 'index.php/';    // Note backslash at the end

3. On the Controller, each time you invoke a method, use site_url() function instead base_url(). You should have loaded the url_helper. See too.

redirect(site_url('entity/method'));

That's all.

Views example:

<li><a href="<?php echo site_url('entity/method')?>">Method</a></li>

Less clean solution: always concatenate the index_page() with base_url(). Do not forget to ensure the backslash before method.

redirect(base_url().index_page().'entity/method');

Explanation

Typically in production environment, you will leave empty a index_page parameter, perhaps base_url parameter too, and will use the ".htaccess" file. Then, site_url() function or alternative base_url()+index_page() will return a recognizable correct address.

Community
  • 1
  • 1
Diego Soto
  • 375
  • 3
  • 8
0

I had it working on the localhost, but I wanted to make it like that so I can easily upload it to the hosting server.

And my solution is this:

$config['base_url'] = ($_SERVER['HTTP_HOST'] === 'localhost' ? '/programiranje' : '/') ;
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

.htaccess:

RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1

I am hosting it locally on windows 10 machine in C:\Apache24\htdocs folder.

Dejan Dozet
  • 948
  • 10
  • 26