My problem is include()
doesn't work in this example:
...
$lang = $_GET["lang"];
$id = $_GET["id"];
if ($lang == "fr"){
include ('indexFr.php?id='.$id);
}
else if ($lang == "ar"){
include ('indexFr.php?id='.$id);
}
else if ($lang == "en"){
include ('indexFr.php?id='.$id);
}
...
I work with this:
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
switch ($lang){
case "fr":
header("Location: indexFr.php?id=".$id);
break;
case "ar":
header("Location: indexAr.php?id=".$id);
break;
case "en":
header("Location: indexEn.php?id=".$id);
break;
default:
header("Location: indexEn.php?id=".$id);
break;
}
But if I want to include something else (not language page) I think this is the right code but, it doesn't work:
include ('www.monsite.com/indexFr.php?id='.$id);
How can I do it?
If your $_GET
array already has a value for id
, you don't need that query string on the end if you're doing an include
. It will use the $_GET
array you already have and get the same $_GET['id']
value.
include
is, in effect, putting the code of the external file into the PHP code that is already running. So, for example, if you have this file:
index.php?id=5
echo $_GET['id'];
include "otherfile.php";
And then this other file:
otherfile.php
echo $_GET['id'];
The output will be:
55
Because you are effectually creating a file that looks like this:
echo $_GET['id'];
echo $_GET['id'];
The include tag doesn't work with query strings because since it's a local file, the query string isn't used.
If you want to include the file from a different domain, you could try:
include ('http://www.monsite.com/indexFr.php?id='.$id);
That said, including files cross-domain is considered bad practice and will possibly just include the generated HTML and not the PHP. If you're including a file from your local filesystem, you really should just use the $_GET
variable that's already there.
You need to specify a full URL for this to work. What you're specifying will look for a file on your local filesystem called indexFr.php?id=123
, which is not what you're trying to do. You need an http:// or https:// in there, so it knows to go through a web-server, which will pass your arguments.
http://www.php.net/manual/en/function.include.php
Indeed, they provide an example case which matches yours quite closely:
// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the
// local filesystem.
include 'file.php?foo=1&bar=2';
// Works.
include 'http://www.example.com/file.php?foo=1&bar=2';