简体   繁体   中英

Laravel 5.1 gives 'undefined index: http_host ' when run 'php artisan serve'

I learned laravel using version 5.4 for 6 months. i installed latest xampp then. But now i got to work on a project built on laravel 5.1 . But when i want to run the app, it gives me the error (undefined index: http_host)! How can i find where the error comes from? How can i solve the problem? I searched online but nothing found fruitful. Can you help me, please? Storage/log.php:

[2017-07-18 21:55:50] local.ERROR: ErrorException: Undefined index: HTTP_HOST in H:\Current\school\school\app\Providers\AppServiceProvider.php:40

Stack trace:

#0 H:\Current\school\school\app\Providers\AppServiceProvider.php(40): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined index...', 'H:\\Current\\scho...', 40, Array)
#1 [internal function]: Erp\Providers\AppServiceProvider->boot()
#2 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Container\Container.php(507): call_user_func_array(Array, Array)
#3 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(734): Illuminate\Container\Container->call(Array)
#4 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(717): Illuminate\Foundation\Application->bootProvider(Object(Erp\Providers\AppServiceProvider))
#5 [internal function]: Illuminate\Foundation\Application->Illuminate\Foundation\{closure}(Object(Erp\Providers\AppServiceProvider), 18)
#6 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(718): array_walk(Array, Object(Closure))
#7 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\BootProviders.php(17): Illuminate\Foundation\Application->boot()
#8 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(203): Illuminate\Foundation\Bootstrap\BootProviders->bootstrap(Object(Illuminate\Foundation\Application))
#9 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(208): Illuminate\Foundation\Application->bootstrapWith(Array)
#10 H:\Current\school\school\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(105): Illuminate\Foundation\Console\Kernel->bootstrap()
#11 H:\Current\school\school\artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#12 {main}  

AppServiceProvider:

public function boot()

{

    if(!Session::get(SITE_ID)){
        $subdomain_name = array_shift((explode(".",$_SERVER['HTTP_HOST'])));
        if(isset($subdomain_name) && !empty($subdomain_name)){
            $domain = $subdomain_name;
        }else{
            $domain = "school";
        }
        $siteToRecollect = DB::table('site_infos')->where('site_alias',$domain)->first();

        if(isset($siteToRecollect->id) && !empty($siteToRecollect->id) && $siteToRecollect->id != 0){

            Session::put(SITE_ID,$siteToRecollect->id);

        }else{

            Session::put(SITE_ID,1);

        }

    }

}

40 line:

$subdomain_name = array_shift((explode(".",$_SERVER['HTTP_HOST'])));

The variable $_SERVER['HTTP_HOST'] only work on browser, not PHP-CLI, so when you run command php artisan $_SERVER['HTTP_HOST'] will not exists, you can check here Undefined index HTTP_HOST even though it is checked

In this case, you can fix your code by checking if $_SERVER['HTTP_HOST'] exists.

if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){
    $gethost = explode(".",$_SERVER['HTTP_HOST']);
    $domain = array_shift($gethost);
}else{
    $domain = "school";
}

About lines will solve your issue, and with a bit of improvement. Here is the full version of fixed code and tested.

public function boot()
{
    if(!Session::get(SITE_ID)){
        $domain = "school";
        if(isset($_SERVER['HTTP_HOST']) && !empty($_SERVER['HTTP_HOST'])){
            $gethost = explode(".",$_SERVER['HTTP_HOST']);
            $domain = array_shift($gethost);
        }
        $siteToRecollect = DB::table('site_infos')->where('site_alias', $domain)->first();
        if(isset($siteToRecollect->id) != 0 && !empty($siteToRecollect->id))
        {
            Session::put(SITE_ID, $siteToRecollect->id);
        }else{
            Session::put(SITE_ID,1);
        }
    }
}

Hope this help,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM