开发者

PHP, URI routing, switch statement and headers

I am trying to get a PHP URI routing system to work.

I am getting the following error when the $page_url = 'services'; line is commented.

PHP Warning:  Cannot modify header information - headers already sent by (output started at PATH:12) in PATH on line 22

Line 12 is the var_dump and line 22 is the 4开发者_开发百科04 headers.

If I uncomment the $page_url = 'services'; I don't get an error.

Commented or uncommented, the var_dump outputs the same thing : string(8) "services".

The page I'm loading is http://testwebsite.com/fr/services

Here is the index.php code.

<?php

// get requested uri
$uri = explode('/', strtolower(substr($_SERVER['REQUEST_URI'], 1)));

//set variables
$lang = $uri[0];
$page_url = $uri[1];

//debug
$page_url = 'services';
var_dump($page_url);

// route current page
switch($page_url){

    case 'services':
        $page_id = 'services';
        break;

    default:
        header("HTTP/1.0 404 Not Found");
        break;
}

?>

Here is the .htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule .* /index.php

AddDefaultCharset UTF-8

IndexIgnore *
Options -Indexes

I don't understand why it tries to run the default case when the $page_url variable is determined through parsing of the requested URI but doesn't if it's set manually since in both case the var_dump outputs the exact same thing.

Furthermore, if I add echo'default'; to the default case, it doesn't output it but I still get a warning in the log. It makes no sense to me.

Thanks for your help.

Edit

It should be noted that if I add echo'services'; to the services case, it gets outputted. I still get a warning on the header though. As if the break; isn't registering. But it is since it doesn't echo "default" either.


My guess is that there is some trailing whitespace in there. What does strlen say about $uri[1]? I'll guess it isn't 8.


And you've echo'd $uri[1] to make sure it is being set to 'services'?


An HTTP response has two parts, headers and body:

----
200 - OK
Content-type: text/html
Content-length: 42
Header: blah blah blah
Header: blah blah blah
Header: blah blah blah
Header: blah blah blah

This is your HTML content page here!

See that blank line separating the two? Focus on it. Zen in on it. That's the "trigger" that headers are "done" and here comes the body.

var_dump sends stuff out to the browser as text.

If PHP is going to send that text out, it has to send the headers, and then the BODY with the var_dump output.

Later, you try to send out another header!

It's too late!!!

The headers already went out, were digested by the browser, and it's eating up your HTML body text.

Options: - Turn on output_buffering in php.ini - use http://php.net/ob_start - change var_dump() to error_log(print_r($whatever, 1)); and tail your apache error_log

The first two have consequences I don't care for, but it's your site.

The last one means you'll be seeing other error messages you probably are already missing, so is BEST imho.


Figured out the problem. The browser was trying to load favicon.ico which was being redirected to the index.php routing page. Since favicon.ico isn't a route, it ran the default case for the favicon.ico and the right one for the actual page load.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜