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;
}
Related
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 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
I created a site where you need to login to visit the different pages, nothing special.
To keep the user logged in, I'm setting the session on top of every page.
My problem is, I don't wanna have to set the different session variables on top on each page. I'd rather have one function I can call to set them. Plus I don't need all those variables on each page, so I'd like the function to accept optional parameters (like the email, or profile picture that are not used on every page).
I call this on top of each page:
<?php
require_once 'session.php';
confirm_logged_in();
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$picture = $_SESSION['picture'];
$group = $_SESSION['group'];
?>
I would like to make it more like this and be able to set only the variables I need:
<?php
require_once 'session.php';
confirm_logged_in();
set_variables($username, $email);
?>
The 'session.php' file is like this:
<?php
session_start();
function logged_in(){
return isset($_SESSION['username']);
}
function confirm_logged_in(){
if(!logged_in()){
header('location: start.php');
}
}
?>
I've tried a few things, but it just led me to huge amounts of errors.
Has someone already done this or found a script doing this? Is that possible?
First of all, if what you want to do is overload your function, you can't do that. For more info on that see this. However, you can do this:
<?php
set_variables($username, $email, $picture,$group)
{
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['picture'] = $picture;
$_SESSION['group'] = $group;
}
?>
Put this function in your session.php file.
I am not sure if I understood right, but if I did, all you need to do is create a new file, let's call it "Session_Variables.php".
After you created the file, paste the following code into it:
<?php
require_once 'session.php';
confirm_logged_in();
$username = $_SESSION['username'];
$email = $_SESSION['email'];
$picture = $_SESSION['picture'];
$group = $_SESSION['group'];
?>
Then, finally, just replace the old code with:
include("Session_Variables.php");
Not directly related to the question you are asking, but you should really add exit; after a redirect header. Clients can ignore headers and still load your page even while not being logged in.
if you want to make set_variables($username, $email) work like i think you wanted, you need to write something like this.
Session.php
<?php
session_start();
function logged_in(){
return isset($_SESSION['username']);
}
function confirm_logged_in(){
if(!logged_in()){
header('location: start.php');
}
}
//Only Picture and group are Optionals
function set_variables($username, $email, $picture = '', $group = ''){
//you can check here is thoses variables are set or valid before assign them
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['picture'] = $picture;
$_SESSION['group'] = $group;
}
//create a function that we need to retrieve thoses values
function get_variable($name){
if ( isset( $_SESSION[$name] ) ) return $_SESSION[$name];
return FALSE; //if the variable is not set.
}
?>
And you can use it like this
<?php
require_once 'session.php';
confirm_logged_in();
set_variables($username, $email);
$username = get_variable('username');
?>
I think you need to move the session_start(); to the actual page. Using a require_once on the session_start(); is not a good plan.
I'm trying to import some variables from a PHP script. It seems simple but I cannot get it to work.
The script contains some global variables like that:
$server_hostname = "localhost";
$server_database = "kimai";
$server_username = "root";
$server_password = "";
$server_conn = "mysql";
$server_type = "";
$server_prefix = "kimai_";
$language = "en";
$password_salt = "7c0wFhYHHnK5hJsNI9Coo";
Then in my script, I would like to access these variables, so I've done:
require_once 'includes/autoconf.php';
var_dump($server_hostname);
But this just outputs NULL. I've also tried:
require_once 'includes/autoconf.php';
global $server_hostname;
var_dump($server_hostname);
but still not working.
I've added some echo statements in the "autoconf.php" file so I know that it's being loaded.
Any idea how I could access these variables?
You have to define the variable as global first:
global $server_hostname;
$server_hostname = "localhost";
It turns out the file was included somewhere else in the application so when I was calling require_once, the file was not being included at all. I changed it to just require and now it works.
Maybe the file was not included properly.
require_once 'includes/autoconf.php';
check current work directory where you include autoconf.php
try this
if (file_exists('includes/autoconf.php')) require_once 'includes/autoconf.php';
else echo 'File not exists';
to check it out.
How about using a constant?
define("server_hostname","localhost");
define("server_hostname","localhost");
If you include the file and the variable is in a plain text, and not inside a function / class it works without the global
Go to your php.ini and put display_errors=On and errors to E_ALL so you will see which is the correct reason
This is evil, but it may get the job done.
<? //PHP 5.4+
\call_user_func(static function(){
$globals = \get_defined_vars();
include 'includes/autoconf.php';
$newVars = \array_diff_key($globals, \get_defined_vars());
foreach($newVars as $name => $value){
\define($name, $value);
}
});
//Variables defined in file are now constants!
?>
better way to use and correct global variables is first assign value to variable and then declare global.
this is:
$server_hostname = "localhost";
global $server_hostname;
i have a PHP site with the following code in it:
<?php
$p = $_GET['p']
include("$p.inc");
?>
Whenever I send a visitor to a page like index.php?p=contact for example I want the file contact.inc to be included. This works fine.
Now I want a certain file to be included (e.g. start.inc) when the visitor is sent to index.php without any GET variables. However, an error message is returned which tells me that $p is undefined (which it logically is).
I tried fixing this problem by using the isset function like so:
<?php
if(!isset($p)) $p = "start";
else $p = $_GET['p'];
include("$p.inc");
?>
but this doesn't work because now $p always contains the string "start" and I can't send the visitor to index.php?p=contact anymore - it will still include start.inc
Can somebody please help me with this issue?
Thanks in advance!
Explicitly specify the allowable values, obtained from outside.
<?php
$allowed_pages = array(
'home' => 'home.inc',
'contact' => 'contact.inc',
);
$page = #$_GET['p'];
$file = array_key_exists($page, $allowed_pages) ? $allowed_pages[$page] : $allowed_pages['home'];
include($file);
?>
You should white-list your pages anyway, for security. so:
<?php
$p = $_GET['p']
switch($p){
case 'contact':
include("contact.inc");
break;
default:
include("start.inc");
}
?>
Define your $p variable just like this:
$p = array_key_exists('p', $_GET) ? preg_replace('!\W!', '', $_GET['p']) : 'start';
you're checking $p instead of $_GET['p'] so, as $p is never set, you always land at starting page.
anyway you have to sanitize this variable first.
good practice would be like this (assuming pages stored in a "pagedata" folder and have .php extension):
if(isset($_GET['p'])) {
$p = basename($_GET['p']);
} else {
$p = "start";
}
$fileName = "pagedata/$p.inc.php";
if(is_readable($fileName)) {
include($fileName);
} else {
include("pagedata/404.html");
}
You should prefer an array-map or a switch like Nanne suggested.
At the very least use basename() if you want to keep using the $p variable directly in the include statement. And this is how you could avoid the "error" (which is a debug notice, btw):
<?php
$p = #$_GET["p"] or $p = "start";
$p = preg_replace("/\W+/", "", $p); // minimum filtering
include("./$p.inc");
?>
Thanks to you all!
I combined most of your suggestions to the following piece of code:
<?php
$pages = array(
'start'=>'Start.inc';
'contact'=>'Contact.inc';
'about'=>'About.inc';
};
$p = array_key_exists(#$_GET['p'], $pages) ? preg_replace('!\W!', '', $_GET['p'] : 'start';
$p = ucfirst($p);
$page = "./$p.inc";
if(is_readable($page)) include($page);
else include(./404.);
?>
I particularly like the array-map (as suggested by Alex and mario) for security reasons aswell as the error page idea by Col. Shrapnel.