Include PHP code with the same ID - php

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';

Related

WordPress header location redirect

having a problem here with WordPress. I want to redirect the page to a specific .php file inside a folder (php/adminpage.php) whenever $_SESSION variable is equals to 1. Let's say the session variable is 1:
<?php
if ((isset($_SESSION['login']) && $_SESSION['login'] == '1')) {
header ("Location: php/adminpage.php");
?>
But the browser returns "Not Found". Any ways to get it to work?
UPDATE [SOLVED]: Using the full path works. Thanks to #andrewsi. Working code:
<?php session_start();
if ((isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: wp-content/themes/euro/php/adminpage.php");
}
?>
You're using a relative path:
header ("Location: php/adminpage.php");
That will look for a folder below where the current file is, and look for adminpage.php in there.
But WordPress does some funny things with page names and .htaccess, so your current page might not be where you expect it to be; it's generally always better to use a full path, which is a lot less ambiguous:
header("Location: /wp-content/themes/euro/php/adminpage.php");
And don't forget to exit after calling it, too, so code execution stops on the page from which you are redirecting.
Is this an actual URL location?
header ("Location: php/adminpage.php");
To my eyes it seems like a file system path. Because is your local setup at this URL:
localhost
And then this would be the location of that file?
localhost/php/adminpage.php
Also, I would clean up your code like so:
<?php
if (array_key_exists('login', $_SESSION) && isset($_SESSION['login']) && intval($_SESSION['login']) == 1)) {
header("Location: php/adminpage.php");
}
?>
By using array_key_exists you prevent unset index errors & by using intval you are assured there is a numerical value in place.

If URL contains 'foo' find and include a matching PHP include

I've written some code that includes 'home.php' if the URL contains 'index.php':
<?php if($_GET['page'] != "index.php") {
include('_includes/home.php'); } ?>
This works fine for a specific page (in this instance the home page), but I want to extend this logic for any page on my site. For instance if the URL contained 'foo2.php' I'd want the the PHP to search for and include '_includes/foo2.php'.
I'm new to php so any help would be appreciated.
if the URL was:
http://example.com/?page=foo2
if (empty($_GET['page'])){
include ("_includes/index.php");
exit;
}
$Page_Search = glob("_includes/*.php");
if (in_array($_GET['page'],$Page_Search)){
include ("_includes/$page.'.php');
exit;
}
This might be of use.
How about:
if(isset($_GET['page'])) include "_includes/{$_GET['page']}.php";

How do I dynamically include a PHP script?

I'm writing code in PHP that requires including a config.php file depending on the website URL.
I've the website URL in the variable $site, and the config for $site is in the directory configs/$site/config.php.
How can I require this file dynamically in PHP?
Is it safe to do include "configs/$site/config.php";?
If you limit $site to a set of values, yes.
$sites = array(
'siteA',
'siteB'
);
if (isset($sites[$site])) {
include "configs/$site/config.php";
} else {
throw new Exception("Unknown site");
}
But remember: Never trust anything from outside, always validate values, that come from a client (browser).
That is fine. Just make sure the $site variable cannot be changed by the user! If the user can change it, he can make it include any file.

separate contents using get parameter

How could I make my webpage change its content using get parameter? I saw a bunch of website registrations that when you are successfully registered you'll be redirected to the same page but with get parameters i.e www.register.com?do=success, or something like that. I tend to make another webpage for that but this looks promising. Instead of making another page, I would just change the content using get parameter for success registration page.
Nowadays web applications have more or less only one single entry point (index.php) and avoid different and resource specific files. The reason is that a single file provides more control over how the user interacts with the site. The actual resource to be returned to the user is then specified in the query string (or determined through trailing folders and some rewriting mechanism).
A simple approach to establish a single entry point for your entire site:
index.php
<?php
switch ($_GET['pageId']) {
case 0:
echo "Home";
include "content/home.inc.php";
break;
case 1:
echo "Links";
include "content/links.inc.php";
break;
case 2:
echo "Animated Gifs";
include "content/coolstuff.inc.php";
break;
case 3:
echo "Guestbook";
include "content/guestbook.inc.php";
break;
default:
echo "404";
break;
}
Call:
http://localhost/index.php?pageId=0
http://localhost/index.php?pageId=1
http://localhost/index.php?pageId=2
http://localhost/index.php?pageId=3
http://localhost/index.php?pageId=Hohoho
Create an index.php file; in a normal setup that is the file that will be excecuted when calling something like www.yoursite.com/?do=success. The querystring (the part after the ?) parameters can be fetched through the global $_GET array.
W3C: PHP Tutorial
PHP For the Absolute Beginner
PHP: A simple tutorial
Beginners PHP
You just need use the $_GET['do'] to decide what to response to the browser.

PHP changing language version

I have a website which i want to create other language version.
I don't want to create folder for each language. I was wondering it
it's possible to add a combobox on each page or on the main one
so that user can setup the language then using php i will
check the option and show the right version. Any suggesting
to achive that?
If you have a combobox, when the user submits it, store the language in the session (session_start(); has to be called) with $_SESSION['lang'] = $_POST['lang'];. I'd advise you to whitelist languages as such:
session_start();
// define language whitelist
$allowedLangs = array('en', 'de');
// only store the new user language if it's an allowed one
if (isset($_POST['lang']) && in_array($_POST['lang'], $allowedLangs)) {
$_SESSION['lang'] = $_POST['lang'];
}
// define the user language based on session data or use 'en' as default if not available
$userLang = isset($_SESSION['lang']) ? $_SESSION['lang'] : 'en';
// parse some language file according to the language
$translations = // TODO load some file with $userLang here
Of course you should adjust this to your own project and environment. For translation files, you can use a plain PHP file that returns an array like such:
<?php
// en.php
return array(
'some.key' => 'Translation',
);
Then if you include that file, the return value of the include will be the array, so in the above code you could do:
$translations = include 'translations/'.$userLang.'.php';
You then have to output all your text through this $translations variable, like echo $translations['some.key'].
if you wanted to use cookies... in the lang files you would include an array of words or content to use.
<?php
if($_GET['language']){
$lang = (string)$_GET['language'];
setcookie("lang", $lang, time()+3600);
header('Location: '.$_SERVER['PHP_SELF']);
die();
}elseif(!isset($_COOKIE['lang'])){
$lang='en';
}else{$lang=$_COOKIE['lang'];}
switch($lang){
case "en":
include('./lang/en.php');
break;
case "fr":
include('./lang/fr.php');
break;
case "pol":
include('./lang/pol.php');
break;
default:
include('./lang/en.php');
break;
}
?>
you mean something along the lines of
if ($_GET['language']) {
include $_GET['language'] . ".php";
}
and then save the languages in a php-file with there name, or a function depending on what you want to do with it
hey for language version.
have languages in combobox.
maintain your current language in session.
When u change language call an ajax call Update the changed language into session and reload the page.
display page view with respect to session stored language.
thats it........

Categories