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

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

Related

How to load the result of a php function into a variable

I have a php file on my server that takes in two inputs through the URL and then comes back with a result. When a page is loaded, I'd like to have the result of that calculation already loaded. For example:
$var = load("http://mysite.com/myfile.php?&var1=var1&var2=var2");
I know that load isn't a real function for this, but is there something simple that suits what I'm looking for? thanks
Use file_get_contents
$foo = file_get_contents('http://mysite.com/myfile.php?&var1=var1&var2=var2');
Or, a better solution if the file is located on your server:
include('myfile.php');
and either set the $_GET variables in the included script itself, or prior to including it.
If they are running on the same server, consider calling the script directly?
$_GET["var1"] = "var1";
$_GET["var2"] = "var2";
include "myfile.php";
You could use file_get_contents, but it may be a more practical solution to simply include the file and call the function directly in the file, rather than trying to manually load the file.

Set $_GET['foo'] variable when calling a PHP script on CLI

When testing and developing some scripts, I would like to be able to define certain $_GET variables.
When a script is called trough mod_php/apache these variables will be defined by adding ?foo=bar to the url.
Is this possible at all?
No, Its not possible via CLI. However you can manually assign the value to the $_GET variable.
OR you can use the command line arguments and assign them to the $_GET.
$_GET['data'] = $argv;
^^That's a little bit manageable..
You can just do $_GET['foo'] = 'bar'; to set it.
Assuming you have some code that is using $_GET['foo'] to do something then the best way to handle it would be to extract that code into a class/method/function and then have context-specific scaffolding which gathers the relevant data from the environment and passes it into the code-block to do it's stuff.
So let's say you had an inline PHP script like this:
$foo = $_GET['foo'];
// do what I need to do with $foo
Then you could wrap this into a function
function doBar( $foo ) {
// do what I need to do with $foo
}
Your inline php script would now be
$foo = $_GET['foo'];
doBar( $foo );
And you could very easily write a CLI script to test this, either by setting $foo directly
$foo = 'test value';
doBar( $foo );
Or by parsing the CLI inputs and getting foo from there instead.
Basically, your code that does stuff (the Model in a traditional MVC) is isolated from its environment and can be used via an HTTP request, in a CLI script, in a unit test etc.
One thing I would stay clear of is assigning your own values to the $_GET and $_POST superglobals. It's smelly and a short-cut to giving you problems some way down the line.
The two options to set $_GET variables: simply have something like:
<a href='www.example.com/index.php?foo=bar'>blah</a>
This will set $_GET['foo'] to bar.
Or:
<form method='get' action='index.php'>
Any information you will send with this form will be retrievable with $_GET.

Passing Variable FROM Included PHP File To Parent Script

I include a PHP file to the HEADER of my WordPress site, which searches through a CSV file and returns me the variable I need. I am trying to have it included in the header because it's a variable I will need throughout the site later on. If I test it out and try to echo this variable from the included script, it works fine. However, in the rest of the site, if I try to call that variable it doesn't return anything.
I know that the variable is being created because I try to echo it and it works. But when the I try to use that variable from a different script, it doesn't work. Is there some kind of code I need to pass the variable over to the rest of the site?
Variables default to function level only, you have to pass them or globalize them if you want to use them elsewhere. Depending on how your script is laid out, you might make it an object property, in which case it will be available anywhere your object is available and in all methods of that object - another option is to use global $var, but that's a bad idea and bad coding practice - another is to put it into a session using $_SESSION['myVar'] = $var and then call it the same way - yet another way is to pass it through arguments such as $database->getRows($var) and then on the other side "public function getRows ($var)", now you have $var in that function by passing it.
Make sure you global $variable the variable everytime you want to use it in a new function, or even within a new script. This will make sure that the variable is available everywhere that you need it.
3 files:
a.php:
<?php
include("c.php");
var_dump("c is ".$c . " after include()");
function incit(){
include("b.php");
var_dump("b is ".$b . " inside incit()");
}
incit();
var_dump("b is ".$b . " after incit()");
?>
b.php:
<?php
$b="bear";
?>
c.php:
<?php
$c="car";
?>
output looks like this:
string(24) "c is car after include()"
string(24) "b is bear inside incit()"
string(19) "b is after incit()"
so $b is only defined INSIDE the scope of the function while $c on the other hand is "globally" definde.
So you have to watch in what scope you are using the include.

PHP Require and Include GET

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

PHP include with HTTP get paramters

How would one go about using php include() with GET paramters on the end of the included path?
IE:
include("/home/site/public_html/script.php?id=5");
How would one go about using php include() with GET paramters on the end of the included path?
You could write into $_GET:
$_GET["id"] = 5; // Don't do this at home!
include(".....");
but that feels kludgy and wrong. If at all possible, make the included file accept normal variables:
$id = 5;
include("....."); // included file handles `$id`
You don't, include loads files via the local filesystem.
If you really wanted to, you could just do this, which would have the same result.
<?php
$_GET['id'] = 5;
include "/home/site/public_html/script.php";
?>
but then you might as well just define the variable and include it
<?php
$id = 5;
include "/home/site/public_html/script.php";
?>
and reference the variable as $id inside script.php.
Well you could use:
include("http://localhost/include/that/thing.php?id=554&y=16");
But that's very seldomly useful.
It might be possible to write a stream wrapper for that, so it becomes possible for local scripts too.
include("withvars:./include/that/thing.php?id=554");
I'm not aware if such a solution exists yet.

Categories