Why does $ _GET not work in the header function?

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://www.example.com/english/404.php?uri=somedata 

or with another language:

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

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://www.example.com/**english**/404.php?uri=somedata 

and I will send $_POST to change the language to German:

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

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

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

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> 
+6
source share
4 answers

If I understand 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 the submit form button. To do this, you must set the action in the tag to include uri from the current page, so that it goes to page 404 of the new language.

 <form action="<?php echo $form_action . ((isset($_GET['uri']))?$_GET['uri']:""); ?>" method="post"> 
+1
source

To expand jwheel12 answer. The form action will work as a relative path without http: // in it. Therefore, adding your action = "" to the current URL. Use the absolute path whenever possible.

 <form action="http://www.example.com/english/404.php<?php echo ( $_SERVER['QUERY_STRING'] )? "?".$_SERVER['QUERY_STRING']) : "" );?>" method="post"> 

or alternatively you can pass variables as a hidden input type

 foreach ($_GET as $key => $value) { echo '<input type="hidden" name="'.$key.'" value"'.$value.'">'; } 

I like to use a custom function like the one below that will go through all the elements of the $ ary array and return the last one that is not empty.

 $myVariable = getVar('myVariable'); function getVar($var) { $ary = array('_SESSION', '_COOKIE', '_POST', '_GET'); foreach ($ary as $a) { if ($GLOBALS[$a][$var] != '') { $return = $GLOBALS[$a][$var]; }; }; return $return; }; 

Finally, you can use preg_replace with a hyperlink if you just want to switch between the two languages

 $currentURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; if (preg_match('/german/i', $currentURL)){ $newURL = preg_replace('/german/i', 'english',$currentURL); $currentLanguage = "german"; } else { $newURL = preg_replace('/english/i', 'german',$currentURL); $currentLanguage = "english"; }; echo '<a href ="'.$newURL. '">'($currentLanguage == "german"? "View in English": "Sehen Sie auf Deutsch";). '</a>'; 
+1
source

From the documentation:

 Note: HTTP/1.1 requires an absolute URI as argument to ยป Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself: <?php /* Redirect to a different page in the current directory that was requested */ $host = $_SERVER['HTTP_HOST']; $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\'); $extra = 'mypage.php'; header("Location: http://$host$uri/$extra"); exit; ?> 

In addition, I believe that http: // is required for FireFox and other browsers with HTTP 1.1 complaints.

Perhaps try something like this?

0
source

Wouldnโ€™t it be easier to stay on one page but change the content? if the language is English, download 404 English information, for German download German, etc.

Just a thought. I can imagine that this can be impractical for large sites.

0
source

Source: https://habr.com/ru/post/945245/


All Articles