PHP Require and Include GET - php

I would like to require a file but also pass GET variables through the url, but when I write:
<?php
require_once("myfile.php?name=savagewood");
?>
I get a fatal error. How would I accomplish this functionality in a different way, such that I don't get a fatal error?

variables will be available as normal you do not have to pass like this.
$name='savagewood';
require_once("myfile.php");
$name will be available in myfile.php

<?php
$getVarsArray = $_GET;
$postVarsArray = $_POST;
/* n number of variables and lines of code*/
include('script-a.php');
?>
Now in script-a.php has access to $getVarsArray and $postVarsArray and if in any case you are in doubt you can use $GLOBALS to access any variable throughout the life cycle of a script. But using global variables is a sin. :)

It is not necessary to pass the variables to the new file, because by including the new file the variables are maintained.
Remember that $ _GET is an array, and it can be modified within the script.
<?php
$_GET['name'] = "savagewood";
require_once("myfile.php");
?>
On this case, $_GET['name'] is accesible from the "myfile.php"

I think I have got a perfect solution to your problem. You can use implode function of PHP. But I would strongly recommend doing Shakti Singh's code.
SOLUTION CODE
echo implode(file('http://path-to-your-site/your-dir/myfile.php?name=savagewood'));

Related

How can I access a script out of root by http protocol?

I have this folder-structure:
\out
\script.php
\root
\include
\calculator.php
I need that script into \calculator.php. How can I include it?
// calculator.php
require( what path ? );
..
Note: I can include it by this path: ../out/script.php. But I also need to pass a argument to script.php. So I want something like this:
.. what path/script.php?arg=value
And as you know, because I need to pass a argument to that, so I have to use http protocol. All I want to know, How can I use both http and ../? Something like this:
http://../out/script.php?arg=value
You don't need to pass parameters when you include the script because it's loaded into the same scope as the first script. This means that the script ../out/script.php has access to the query string arguments through the $_GET variable:
$_GET['arg']
Also, any variables you define in calculator will be available in script.php
You could instantiate some variables inside the $_GET superglobal before you include your file, i.e.
$_GET['arg'] = "value";
require('../../out/script.php');
But that isn't ideal, in my opinion.
If I were you I'd use a boring 'ol global variable (assuming you start in the global scope):
$arg = 'value';
require('../../out/script.php');
Then simply use it like any other variable (maybe with some sanity checking, though):
if (!isset($arg)) {
die('Oh my! $arg is not set! Error! Danger!');
}
do_something_with($arg);

PHP - how to send variable values without Global variables between php files?

Without use of cookie, session, post, get superglobals, is there a way to retrieve variables between php files?
1.php has
$value="hello";
and
2.php wants to retrieve
$value // with value hello
TRY this:
1.php
$a="this is my 1.php";
2.php
include("1.php");
echo $a;
OUTPUT:
this is my 1.php
Here's an example using a class...
1.php
<?php
class Config {
public static $test = "hello world!";
public static $arrayTest = array(
'var1'=>'hello',
'var2'=>'world',
);
}
?>
2.php
<?php
include('1.php');
echo Config::$test;
echo Config::$arrayTest['var1'];
?>
You will have to store the state of the variables somewhere. If you don't want to use the session, you can write them to a file or database. Or, you can store them client-side using JavaScript. You can't read between two different requests without storing the information, though.
Here is a common method I use, because you can write to it as well, making it dynamic and not hard coded values that require you to manually edit the file.
globalvalues.php
<?
return array (
'value1' => 'Testing'
);
2.php
$globalValues = include('globalvalues.php');
echo $globalValues['value1'];
I have wrapper classes around this, but thats the basics of it.
You could make a class, then include the class and reference the variables through that class.
If they are run in the same call, then you can include the PHP file that defines the variable in the second PHP file and access it as if it was defined in the second one.
If these scripts are executed as part of 2 different calls, then you need to give us more information about what / why you are trying to do.

How do I pass a php variable to a .php include?

I have a file, lets say it's index.php where the very beginning of the file has an include for "include.php". In include.php I set a variable like this:
<?php $variable = "the value"; ?>
then further down the in index.php I have another include, say "include2.php" that is included like this:
<?php include(get_template_directory_uri() . '/include2.php'); ?>
How can I call the "$variable" that I set in the first include, in "include2.php"?
The exact code that I am using is as follows:
The very first line of the index.php I have this line
<?php include('switcher.php'); ?>
Inside switcher.php I have this
<?php $GLOBALS["demo_color"] = "#fffffe"; ?>
If I use this in index.php, it works
<?php echo $GLOBALS["demo_color"]; ?>
However, If I use the following code to include another php file
<?php include(get_template_directory_uri() . '/demo_color.php'); ?>
then inside demo_color.php I have this code:
<?php echo "demo color:" . $GLOBALS["demo_color"]; ?>
The only thing it outputs is "demo color:"
edited for code-formatting
It simply can be used in include2.php, unless the inclusion of include.php happens inside of a different scope (i.e. inside a function call). see here.
If you want to be completely explicit about the intention of using the variable across the app, use the $GLOBALS["variable"] version of it's name, which will always point to the variable called variable in the global scope.
EDIT: I conducted a test against php 5.3.10 to reconstruct this:
// index.php
<?php
include("define.php");
include("use.php");
// define.php
$foo = "bar";
// use.php
var_dump($foo);
This works exactly as expected, outputting string(3) "bar".
<?PHP
//index.php
$txt='hello world';
include('include.php');
<?PHP
//include.php
echo $txt; //will output hello world
So it does work. Though there seems to be a bigger issue since this is likely to be difficult to maintain in the future. Just putting a variable into global namespace and using it in different files is not a best practice.
To make the code more maintainable it might be an idea to use classes so you can attach the variables you need explicit instead of just using them. Because the code around is not showed it is not clear what is your exact need further but it will be likely the code can be put in classes, functions etc. If it is a template you could think about an explicit set() function to send the variable data to the templates and extract() it there.
edit:
In addition based on the information first set your error_reporting to E_ALL and set the display_errors to 1. So you get all errors since the information you placed in your updated question gives indications that a missing variable is used as a constant which should raise errors all over the place.

Passing a variable to PHP with jQuery .load

I have a page ("main.php") which loads content from an external PHP file ("rpc.php"). Using the below syntax on main.php successfully pulls in content from rpc.php:
$("#portfolioContent").load("rpc.php?o="+day+"");
On rpc.php I have an if statement (part of a long switch function), as follows:
if ( $pagename == "home" ) {
break;
}
This break is not occuring because the variable has not been set. rpc.php is used by various parent pages so the variables need to be set on those. On a parent page I have tried using the following code to attempt to set the variable and pass it to rpc.php but to no avail:
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
Can anybody point me in the right direction? Thank you.
It should be like this,
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");
Your syntax is wrong. Try this:
$("#portfolioContent").load("rpc.php?o="+day+"&pagename=home");
Notice i substituted the $ with a &
Cheers
change this line
$("#portfolioContent").load("rpc.php?o="+day+"$pagename="home"");
to this
$("#portfolioContent").load("rpc.php?o="+day+"pagename="home");
then access the variable with $_GET['pagename']
To access variables from the URL in PHP just do:
$_GET['variablename']
For example, with the URL http://www.example.com?hello=hellWorld
echo $_GET['hello'];
would print helloWorld
Also, you use & to separate variables, not $

PHP: include(...); in a variable

I'm trying to include my AdSense code inside a variable like this:
$ad = include_once('../adsense/ad.php');
echo $ad;
In ad.php I'm doing this:
<?php
return '<script>...</script>';
?>
It's showing my AsSense ad but only once. After that, it's outputting 1. Am I missing something here?
You called include_once(), so the file will only be included one time. After that, it will return TRUE because the call succeeded, but no new content was included. Use plain include() instead if you need to use it multiple times.
$ad = include('../adsense/ad.php');
use include instead of include_once. That, or only call include_once once ;)
I think you may be better off using a function for this. You can get the same result, in a much more 'conventional' manner.
Using return in an include will work with include instead of include_once, as mentionned in other answers, but keep in mind include used in this manner makes for non-conventional code structures, harder to re-use, won`t show up in function list definition, among other inconveniances.

Categories