I already have a question with this problem, but another question related to $_GET and header-function:
I have a multilingual site. When a page that does not exist is called, .htaccess will link to 404.php in the root directory.
.htaccess looks like this:
ErrorDocument 404 /404.php
The fact that 404.php in the root direction just looks at which language is set to SESSION or COOKIE by default and belongs to the /language/404.php language /language/404.php
The root 404.php as follows:
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");
Referring to the language subdirectory, the published URL looks like where the uri parameter is still different:
http:
or with another language:
http:
The code /english/404.php as follows:
<?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"; ?>
A template is just an HTML style. The footer that will be included in template_404.php is the main attraction for sending data. This footer has the ability to change the language setting and switch between language paths. If the form is submitted, SESSION and COOKIE will be changed and the page will start the 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 the page has a URL of
http:
and I will send $_POST to change the language to German:
http:
for example $_GET['uri'] will be empty and the URL look like this:
http:
after starting the header function. Instead of a header, I tried to display this line, and I will get a message that uri 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=
It is strange that when I send out the page before sending $_POST and look at the source code of the site, the $uri variable will correctly read the $_GET parameter, but later this will no longer work in the header function. The question is why this does not work in this?
So it would be nice if someone tells me what to do to get this problem. I really would appreciate it.
Thanks a lot.
UPDATE:
I tried to save the $_GET parameter in SESSION , but when submitting the form and redirecting to the website of another language, the contents of SESSION seem to be wrong, because it will not be $_GET instead it will be something that comes from the css link from the html head, like css/colorbox.css
I'm currently trying to get this by setting the form action to this:
... 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>