I believe my problem should be simple, but no matter how much looking I do I cant find a solution :(
I am trying the following....
File structure.....
-Folder 1
-Folder 2
-loader.php
-admin.php
loader.php contents...
<?php // $_GET['l'] == admin.php
$location = $_GET['l'];
require $location;
?>
admin.php contents .....
<?php echo "test"; ?>
But this doesnt work. Anything I search only brings results regarding passing the variable to the loaded file. I'm wanting to use the variable to define the location of the file to require.
Surely this is possible somehow!
<?php // $_GET['l'] == admin.php
$location = $_GET['l'];
require $location;
$location = $_GET['l'];
require '$location';
?>
i have also tried require|include|require_once|include_once
Related
I am trying to create a multilingual website
I may be going about this the wrong way but here it goes
So just to test first in index.php i have:
<?php
$CURRENT_LANG = "en";
require'./includes/essentials/config.php';
include_once'./includes/essentials/header.php';
include_once'./includes/plugins/search-bars/global-nav.php';
include_once'./includes/essentials/footer.php';
php echo $test
?>
in my config.php i have:
<?php
if($CURRENT_LANG == "en"){
include_once $_SERVER['DOCUMENT_ROOT'].'/gps/includes/language/lang-en.php';
}else{
if($CURRENT_LANG == "pt"){
include_once $_SERVER['DOCUMENT_ROOT'].'/gps/includes/language/lang-en.php';
}
}
?>
in lang-en.php i have:
<?php
$test = "Hi";
?>
in lang-pt.php i have:
<?php
$test = "Olá";
?>
The question at hand is how would i make it so that the user can change the var from en to pt so i can have to php files with all the translations needed?
Or is this a really bad way to implement multiple languages?
in the root of my project folder i have a file called index.php and a directory called views which contains a file called page_one.php ...
index.php file has the follwing code:
<?php
$action = "one";
if ($action == 'one') {
$name = "John";
//include './views/page_one.php';
header("Location: views/page_one.php");
}
?>
and page_one.php has the following code:
<?php echo 'Name = ' . $name; ?>
in the above code I have commented out the line with include because that works perfectly..I want to pass the value of $name using the header function..Is there a way of doing it WITHOUT sending the value in the URL?
I want the address in the URL to change when page_one.php is accessed that is why I am using the header function instead of include...
#Qirel as per said, try using session. Do something like this in index.php
<?php
session_start();
$_SESSION['name'] = "John";
header("Location: views/page_one.php);
exit();
?>
and inside page_one.php
<?php
session_start();
echo $_SESSION['name'];
unset($_SESSION['name']); // remove it now we have used it
?>
I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>
I have a three tier tree for displaying content on a page. It uses includes to display specific PHP pages based on the URL. What doesn't happen is the variables are not understood in included PHP files.
index.php
// example url http://fakesite.com/?color=red&user=999
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
global $color;
global $user;
include 'page2.php';
page2.php
global $color;
global $user;
echo 'hi '.$user.'I hear you like '.$color;
There is no need at all for those $global lines. Any variables defined in the main script are defined in the included file. It's basically like taking the code in the included file and shoving it in the place of the include call (with a few exceptions)
This line:
include_once 'page2.php;
should change to:
include_once 'page2.php';
You have a missing quote.
Have you tried removing ALL those four global lines? I don't know if that's the problem, but they're not necessary at all!
When including or requiring a file all the variables declared above are available to the included/required file.
If that didn't solve it, maybe you have the wrong path on include.
index.php
<?php
$user = $_GET['user'];
if ($_GET['color'] == 'red')
{$color = 'red';}
elseif ($_GET['color'] == 'white')
{$color = 'white';}
else
{$color = 'blue';}
include 'page2.php';
?>
page2.php
<?php
echo 'hi '.$user.'I hear you like '.$color;
?>
global example
function dosomethingfunky(){
global $user, $color;
echo 'hi '.$user.'I hear you like '.$color;
}
I have a set of variables that are set outside the target php include file, how would i set them in the target php include file. Example:
<?php
$fname = "david";
?>
Now how would I set $fname in another php file?
In your other PHP file you would do:
<?php
$fname = "david";
?>
This answers your question directly, but I would hazard a guess that you actually want to have access to variables that are set in a file that is included into your current file or something along those lines.
So
File1.php:
<?php
$fname = "david";
?>
File2.php
<?php
require_once 'File1.php';
echo $fname;
Would result in david being printed to screen.
Just use it directly.
$fname = "your value";
The PHP docs have an article about variable scopes.
The global variable scope is shared across all included and required files. In your example, once you've defined $fname globally, all other PHP lines executed afterwards can access $fname even if they're in different files.
Example: if a.php is:
<?php
$fname = "david";
?>
and b.php is:
<?php
$fname = "sarah";
include 'a.php';
// $fname is now "david"
?>
then executing b.php would define $fname as "sarah", then redefine it as "david" (through a.php).