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.
Related
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);
I would like to encode a php page which contains some php functions.
For example, I have a page named: code.php with this functions:
<?php
function data(){
echo "foo";
...
}
function storage(){
echo "storage files..";
...
}
?>
I use these functions in my other php pages and I would like to protect them by other users. How can I encode their code?
I read about base64_encode() but the examples only show how to encode a string: how can I use this solution to encode and decode my php functions?
Thank you!
If you want to stop others from seeing your PHP code you can either make it as hard as possible (via minifying, obfuscating, whatever you wish to call it) or encrypt it.
There's an answer right here on SO with a few suggestions and another I'd add is ion cube.
With encrypted code you're likely to need further changes to your web server such as an apache module. With obfuscation it will just make it harder for the other developers to read, for instance changing variables and functions names to something meaningless and hard to read.
You will inevitably need to keep a copy of your unobfuscated PHP so you can work on it in a sane manner, which may be hard if you're only developing on your server.
To use Base64 you're probably thinking of doing something like this:
eval(base64_decode('ZnVuY3Rpb24gZGF0YSgpew0KZWNobyAiZm9vIjsNCn0NCmZ1bmN0aW9uIHN0b3JhZ2UoKXsNCmVjaG8gInN0b3JhZ2UgZmlsZXMuLiI7DQp9DQokZGF0YSA9ICdkYXRhJzsNCiRzdG9yYWdlID0gJ3N0b3JhZ2UnOw=='));
What's happening here is the Base 64 string is actually valid PHP, and you first decrypt it the eval it. An example of what the decoded string might look like:
function data(){
echo "foo";
}
function storage(){
echo "storage files..";
}
$data = 'data';
$storage = 'storage';
After the above eval call you would then do something like:
// call the data function
$data();
// call the storage function
$storage();
As stated from the documentation:
PHP supports the concept of variable functions. This means that if a
variable name has parentheses appended to it, PHP will look for a
function with the same name as whatever the variable evaluates to, and
will attempt to execute it.
So, calling $someVariable() will try to run a function named whatever $someVariable contains. If you set $someVariable to foo, it would try to run foo(), if you set $someVariable to sausage, it would try to run sausage() and so on.
Obviously bear in mind that you need to make sure these function variables' names aren't going to be used elsewhere.
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.
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.
There is a php file (current.php) with some variables, like:
function do() {
$var = 'something';
}
And one more php file (retrieve.php), which is loaded to current.php with jQuery ajax .load().
The problem is - retrieve.php doesn't see $var.
Tryed this (inside retrieve.php, shows nothing):
global $var;
echo $var;
How to fix?
Thanks.
Some things you must be aware of:
When you use PHP, you do not download a file: you execute a script and retrieve its output.
PHP variables are destroyed when the script ends. You cannot share variables between two scripts unless you store them somewhere (e.g., in a database or session file).
PHP variables are local to the function where you define them, unless you issue a global $foo; statement inside the function.
jQuery is a JavaScript library. JavaScript and PHP are different languages: they cannot see each other variables.
Said that, I suggest you reconsider your question and try to explain what you need to accomplish rather that how you want to implement it.
The problem is - retrieve.php doesn't see $var.
Sure it is!
all current.php variables are long dead along with current.php itself, which was run, print some HTML and die.
you have to pass required value using standard HTTP mechanisms. you know - GET, POST etc.
If you want the script you load through AJAX get the value of the vars from the page initiating the AJAX load then you either have to pass the values when loading the AJAX script or store them somewhere temporarily (in DB linked by session ID, or in a session var) so you can retrieve them easily.