CodeIgniter not working on localhost
Asked Answered
S

6

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!

Sheepwalk answered 7/6, 2013 at 9:30 Comment(3)
403 forbidden error keeps on appearing while running the site on localSheepwalk
edit you're question and add the htaccess. it could helpCoryden
Check the folder for file permissions. Forbidden errors only occur when permission problem occurs...Uranometry
M
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

Mickimickie answered 7/6, 2013 at 10:1 Comment(2)
the OP asked about a website already made and configured, not about a new installation.Coryden
@tomexsans, yes it is a website that's already made and configured, downloaded from ftp. installation worked fine.Sheepwalk
R
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

Rerun answered 7/6, 2013 at 9:31 Comment(2)
actually its localhost, i just changed it to sample_localhost here in stackoverflow as it doesn't allow the "localhost" word in questions :)Sheepwalk
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]Sheepwalk
A
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']);
Aerobiosis answered 16/12, 2015 at 9:45 Comment(0)
B
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.

Before answered 19/1, 2016 at 16:25 Comment(0)
G
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.

Giustino answered 18/9, 2018 at 15:45 Comment(0)
D
0

CI4 Manual Installation Where CI4 has official git repo "https://github.com/codeigniter4/framework" to download.

After downloading and extracting your directory, you will have to make some changes as follows:-

~config/App.php
 public string $baseURL = 'http://localhost/yourFolderName';

~public
 Move .htaccess and index.php to root folder (/youFolderName)

~yourFolderName
 Rename env to .env and edit .env 
 CI_ENVIRONMENT = development // for running localhost working

Note:- If the above instructions are not working then give read and write permission to your folder(yourFolderName).

Deandre answered 3/10, 2023 at 8:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.