Routing problem

I have a router.php file as below:

<?php
class router{
    private $module_folder_name = "modules";
    function Router(){

    }
    function Route()
    {
        $moduleValue = GetParam('module');
        $folder = $this->module_folder_name;
        if ($moduleValue == null):
            {
                header("location: index.php?module=main");
            }
        else:
            {
                switch ($moduleValue) {
                    case "main":
                        include_once $folder."/main.php";
                        break;
                    case "setting":
                        break;
                    default:
                        include_once $folder."/404.php"; //load 404 page
                }
            }
        endif;
    }
}
?>

and these functions:

<?php
function GetParam($param_name){
    if (!empty($_GET[$param_name])) {
        $value = $_GET[$param_name];
        return $value;
    }
    else{
        return null;
    }
}

function PostParam($param_name){
    if (!empty($_POST[$param_name])) {
        $value = $_POST[$param_name];
        return $value;
    }
    else{
        return null;
    }
}

?>

I want to conduct the user to index.php page when they don’t provide module name.
My code gives error as below:

Warning : Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\resume\panel\index.php:39) in C:\xampp\htdocs\resume\panel\controllers\router.php on line 13

Well, I am not sure of your code, but, there are some things you need to think about. PHP needs to have the header sent out first and then the webpages. If you need to do redirects, it must be BEFORE anything is sent to the browser. Redirecting, meaning using "header(‘location:somewhere’); " must be done before anything is displayed on the page. If you did this:

<?PHP
echo "some text";
header("Location: new-page.php");
?>

It will throw out the same error you got. That is because the ECHO sends a header out to the browser stating that the page is a standard HTML page and the text. The browser has started and you can no longer do any redirects. Guessing that line#13 is: include_once $folder."/main.php"; and that might be sending headers out after they were already sent out to the browser.

Recapping, all of your possible redirects must be before your < html > line. You can not send out the top section of your page and then execute a header command. Do all your processing for possible header commands BEFORE you send any menus or beginning of webpages. ( Before < html > tags! )

Hope this helps!

I used ob_start() function and the problem solved.

Doing a workaround hack is not solving the problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service