Welcome Guest, Not a member yet? Register   Sign In
Error while I open CodeIgniter index.php page without parameters
#1

[eluser]gianluborza[/eluser]
Hi, I noticed that if I try to open the CodeIgniter main index page with the URL http://myip/index.php/ instead of http://myip/index.php the page returns the following errors:
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: libraries/Router.php
Line Number: 190
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: libraries/Router.php
Line Number: 196
A PHP Error was encountered
Severity: Notice
Message: Undefined offset: 0
Filename: libraries/Router.php
Line Number: 199
I fixed the problem adding in function _validate_request($segments) of libraries/Router.php the following controll (line 189):
if($segments == null)
{
$segments[0]='';
}
#2

[eluser]Colin Williams[/eluser]
What version of CI? I cannot replicate. In fact, I can do http://www.example.com/index.php//// and it works
#3

[eluser]gianluborza[/eluser]
Version 1.6.3
If I try with one or many '/' I have the same problem...
Maybe it is caused by a bad configuration of my WebServer.
I attach a picture of the error.
#4

[eluser]Colin Williams[/eluser]
My assumption is a poorly formed mod_rewrite implementation or something with routing. Are you using either mod_rewrite or routing?
#5

[eluser]gianluborza[/eluser]
Probably you are right. Unfortunately I do not have access to the Apache configuration of the Web Host (I'm using a free host service). Thank you for the help :-)
#6

[eluser]Unknown[/eluser]
I noticed this too, when I used the sample htaccess file from the documentation (in /user_guide/general/urls.html).

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|user_guide)
RewriteRule ^(.*)$ /index.php/$1 [L]
#7

[eluser]darkbrian[/eluser]
same issue here - you can rid the warning by changing the error reporting in the config.php file
#8

[eluser]Khoa[/eluser]
I think I know what is causing this problem. It works like this:

The problem happens only on my home page when accessing the site on my host at the real domain:
Code:
http://www.mydomain.com/
But it does not happen on my local at:
Code:
http://localhost/mydomain/
Nor does it happen when I append index.php to my host like:
Code:
http://www.mydomain.com/index.php
Or when I go to another sub-page like:
Code:
http://www.mydomain.com/login/
When I look at the line 197 and others inside router library, it says:
Code:
if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
So I think the problem is because when there is nothing beside the domain on the url, the variable $segments is empty array, thus the item $segments[0] does not exist and it raises the error. When we do our testing on local, we won't see this error because the segments array always has at least 1 item that is the mydomain at index 0, the domain is now localhost.

I will try to write an extension to this library to fix this and will post it here once finished.

Anyone has any other suggestion about this bug?

Khoa
#9

[eluser]Khoa[/eluser]
I tried to write an extension to the library and it did not work but when I added the code straight into the core router class, it works! Here is my code:

MY_Router.php

Code:
<?php
if (! defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Router extends CI_Router
{

    function MY_Router ()
    {
        parent::CI_Router();
    }

   // --------------------------------------------------------------------
    
    /**
     * Validates the supplied segments.  Attempts to determine the path to
     * the controller.
     *
     * @access    private
     * @param    array
     * @return    array
     */    
    function _validate_request($segments)
    {
        if (count($segments) == 0)
        {
            $this->set_class($this->default_controller);
            $this->set_method('index');
            return $segments;
        }
    
        // Does the requested controller exist in the root folder?
        if (file_exists(APPPATH.'controllers/'.$segments[0].EXT))
        {
            return $segments;
        }

        // Is the controller in a sub-folder?
        if (is_dir(APPPATH.'controllers/'.$segments[0]))
        {        
            // Set the directory and remove it from the segment array
            $this->set_directory($segments[0]);
            $segments = array_slice($segments, 1);
            
            if (count($segments) > 0)
            {
                // Does the requested controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$segments[0].EXT))
                {
                    show_404($this->fetch_directory().$segments[0]);
                }
            }
            else
            {
                $this->set_class($this->default_controller);
                $this->set_method('index');
            
                // Does the default controller exist in the sub-folder?
                if ( ! file_exists(APPPATH.'controllers/'.$this->fetch_directory().$this->default_controller.EXT))
                {
                    $this->directory = '';
                    return array();
                }
            
            }

            return $segments;
        }

        // Can't find the requested controller...
        show_404($segments[0]);
    }

}
?>

The only thing that I add in is this block at the beginning of the function:
Code:
if (count($segments) == 0)
        {
            $this->set_class($this->default_controller);
            $this->set_method('index');
            return $segments;
        }

But it keeps receiving the same error that offset 0 is not found in the following if checking.

However, if I put above block straight into the router.php file, it works fine.

So that means the block works, but my extension does not.

What do I do wrong in extending the library here? Can anyone help me?

For the sake of the project going live very soon, I have to put it inside the core lib but really want to move it out as an extension.

Thanks.
#10

[eluser]brandonrichards[/eluser]
Try this:


In your .htaccess file:

Comment out anything similar to:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|user_guide)
RewriteRule ^(.*)$ /index.php/$1 [L]


Add:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>

Save it, upload it, Refresh the page




Theme © iAndrew 2016 - Forum software by © MyBB