Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does $_GET not work in header-function?

I already have a question with this issue but a further question belonging $_GET and header-function:

I have a multilingual site. When calling a page that does not exist, .htaccess will refer to 404.php in the root directory.

The .htaccess looks like this:

ErrorDocument 404 /404.php

That 404.php in the root direction simply looks at which default language is set by a SESSION or COOKIE and refers to the language subdirectory /language/404.php

The root 404.php looks like that:

include_once "scripts/pref_language.php";

if (isset ($_GET['uri']) ){
    $get = $_GET['uri'];

    header("Location: /$pref_language/404.php?uri=$get");
    exit;
}

$url = urlencode($_SERVER['REQUEST_URI']);

header("Location: /$pref_language/404.php?uri=$url");

Refered to the language subdirectory, the posted URL looks like this where the parameter uri is still different:

http://www.example.com/english/404.php?uri=somedata

or with another language:

http://www.example.com/german/404.php?uri=somedata

The code of /english/404.php looks like this:

<?php
error_reporting(E_ALL);
ob_start();
session_start();
header ("Content-Type: text/html; charset=utf-8");

include_once "../scripts/db_connect.php";
include_once "../scripts/functions.php";
include_once "../scripts/browser_identification.php";
include_once "../scripts/pref_language.php";

...some text variables in its specific language

include_once "../templates/template_404.php";
?>

The template is just some HTML styling. The Footer that will be included in the template_404.php is the main attraction to send data. In that Footer there is the option to change the language setting and switching between the language paths. If a form will be submitted the SESSION and COOKIE will be changed and the page starts a header-function.

So here is the Code from the Footer:

if (isset($_GET['uri']) ) {
    $uri = urlencode($_GET['uri']);
    echo "TEST URI: $uri";
}

...

$basename = basename($_SERVER['SCRIPT_NAME'], ".php"); 

if ($basename === "index") {
    $form_action = rtrim($_SERVER['PHP_SELF'], 'index.php');
} else{
    $form_action = htmlentities($_SERVER['PHP_SELF']);
}

...

if ( isset($_POST['pref_lang']) ) {

    $content = $_POST['pref_lang'];

    if ($content === "german") {

        if ($basename === "about") {
            $basename = "info";
        } else if ($basename === "contact") {
            $basename = "kontakt";
        }           
    }
    $_SESSION['pref_language'] = $_POST['pref_lang'];

    setcookie('pref_language', '', time()- 999999, '/', '.example.de' );
    setcookie('pref_language', $content, time()+60*60*24*365, '/', '.example.de');

    if ($basename === "404"){

        header("Location: ../$content/404.php?uri=$uri");

    } else {

        header("Location: ../$content/$basename");  
    }
}
?>
<div id="data">
    <fieldset>
        <ul>
            <li>
                <form action="<?php echo $form_action;?>" method="post">      
                    <input type="submit" id="german" name="pref_lang" value="german"/><label for="german">german</label>
                </form>
            </li>
        </ul>
    </fieldset>
</div>

The problem is that when a page has the URL of

http://www.example.com/**english**/404.php?uri=somedata

and I will submit $_POST to change the language to german like:

http://www.example.com/**german**/404.php?uri=somedata

for example $_GET['uri'] will be empty and the URL looks like:

http://www.example.com/**german**/404.php?uri=

after starting header function. Instead of header I tried to echo out that line and I will receive a message that uri is not defined.

Undefined variable: uri in /customers/7/4/1/example.com/httpd.www/scripts/footer_en.php on line 102
Location: ../english/404.php?uri=

The strange about that is that when I will call the page before sending $_POST and have a look at the source code of the site the variable $uri will read out the $_GET parameter correctly but later on it does not work anymore in the header function. The question is why does that not work in that?

So it would be nice if someone can tell me what to do to fetch this issue. I really would appreciate.

Thanks alot.

UPDATE:

I tried to save the $_GET parameter into a SESSION but when posting the form and getting redirected to the other language site the content of the SESSION seems to be wrong because it will not be the $_GET parameter instead of that it will be something what comes from a css link from the html head like css/colorbox.css

At the moment I try to fetch this by setting the form action to that:

...
if ($basename === "index") {
    $form_action = rtrim($_SERVER['PHP_SELF'], 'index.php');
}  

if ($basename === "404") {
    $form_action = "";
}

if  ($basename !== "index" AND $basename !== "404") {
    $form_action = htmlentities($_SERVER['PHP_SELF']);
}
...

<li>
    <form action="<?php echo $form_action. ( (isset($_GET['uri']) ) ? "/english/?uri=".urlencode($_GET['uri']) : "" );?>" method="post">
        <input type="submit" id="english" name="pref_lang" value="en"/><label for="en">englisch</label>
    </form>
</li>
like image 429
bonny Avatar asked May 17 '13 08:05

bonny


People also ask

Why my header is not working in PHP?

The root cause of this error is that php redirect header must be send before anything else. This means any space or characters sent to browser before the headers will result in this error. Like following example, there should not be any output of even a space before the headers are sent.

What PHP can do with header () function?

The header() function in PHP sends a raw HTTP header to a client or browser. Before HTML, XML, JSON, or other output is given to a browser or client, the server sends raw data as header information with the request (particularly HTTP Request).


1 Answers

If I'm understanding this correctly, you want to go from:

http://www.example.com/**english**/404.php?uri=somedata

To:

http://www.example.com/**german**/404.php?uri=somedata

Using a form submit button. To do this you would set the action on the tag to include the uri from the current page so that it's passed to the new language 404 page.

<form action="<?php echo $form_action . ((isset($_GET['uri']))?$_GET['uri']:""); ?>" method="post">
like image 183
jwheel12 Avatar answered Oct 06 '22 21:10

jwheel12