I'm running into a strange problem where $_REQUEST is not showing the value of a parameter I am explicitly passing e.g.
http://example.com/strange.php?parName=1234
strange.php:
<?php
$foo = $_REQUEST['parName'] ;
echo $foo ;
?>
I have looked in the Inspector and the Network tab actually shows the correct query string parameter.
I have carried out some tests.
in test.html:
link
in request.php:
$foo = $_REQUEST['parName'];
echo $foo ;
I have also tried:
$foo = $_GET['parName'];
echo $foo ;
Both of which work.
Therefore thinking about the strange issue you are encountering, you will need carry out various checks:
Is the query string getting passed? See $_SERVER["QUERY_STRING"]
Check the request method $_SERVER["REQUEST_METHOD"]
Check to see if the parName variable isset
if(isset($_GET['parName'] )) {
$foo = $_GET['parName'];
echo $foo ;
}
else { "echo query string not received";
}
Check your error log to see what Apache is saying?
What data is actually contained in the parName variable?
var_dump($_REQUEST['parName'])
Once you've run all these tests, and the issue is still not resolved, please post your results so we can consider other possible paths to solving this problem.
Related
Similar questions have been asked before, but after following several working examples closely I cannot get this to work for me.
I am trying to access the variable $nameValues that was declared in insert-name.php
insert-name.php is the first script that runs:
error_reporting(-1);
$nameValues = "'".implode("', '", array_values($nameArray))."'";
print_r($nameValues); // returns the expected variable
and insert-answer.php is the second script to run:
error_reporting(-1);
require "insert-name.php";
if (isset($nameValues)) {
print_r("nameValues variable:" . $nameValues);
} else {
echo "variable nameValues does not exist";
}
//returns nameValues variable:'' indicating that $nameValues exists but is empty
Both .php files are stored in the same folder on my webserver. I've tried using both require and include statements. I successfully use a require statement to connect to my database in both scripts, not sure why it is not working for insert-name.php.
Edit:
No errors are reported.
If statement returns "nameValues variable:'' " indicates that $nameValues variable exists but is empty.
I also tried replacing require "insert-name.php"; with require __DIR__."/insert-name.php"; which returned "variable nameValues does not exist" from the if statement.
Because neither gave a fatal error bothrequire statements successfully accessed the insert-name.php file, but I don't know why one method thinks $nameValues doesn't exist and the other thinks $nameValues is empty.
There is no problem accessing the variables from another file like you have provided they are in the same scope (one is not inside a function etc.). In your case it looks like it is just a case of how you are calling print_r()
When you do print_r('nameValues variable:" . $nameValues) you are converting $nameValues into a string.
Try something like this to dump your $nameValues:
error_reporting(-1);
require "insert-name.php";
if (isset($nameValues)) {
echo "nameValues variable: ";
print_r($nameValues);
} else {
echo "variable nameValues does not exist";
}
As you said:
returns nameValues variable:''
There was one detail I overlooked, the quotation marks at the end. When you echo a string in PHP it only prints the content (there are no quotations printed, unless they are in the string).
Effectively this all just means that $nameArray is empty, so check the script that generates this. Also, because of how implode() works, array_values() is completely redundant here.
As simple way to test this is echo count($nameArray);. If the output is "0" the array is empty.
I am trying to encapsulate isset() and empty() in a function.
It worked fine on my home development sever (apache 2.4.3, PHP 5.2.17 under WinXP). When I ported it to my university Linux machine running Fedora, I got a notice about undefined index.
I checked my php.ini on my home computer, and error reporting is set to all. I put error_reporting(E_ALL); in my PHP script to try duplicate the error. It didn't happen.
Question 1: Why am I not getting the notice on my home development computer?
Here is my function:
<?php
function there($s) {
if (! isset($s)) {
return false;
}
if (empty($s)) {
return false;
}
return true;
}
?>
Here I test if a session variable exists:
if (! there($_SESSION['u'])) {
$_SESSION['u'] = new User();
}
I switched my function so that I test empty() before I test isset() thinking this will avoid getting the notice. I haven't had a chance yet to test this at school.
Question 2: Does empty() in general avoid giving a notice if the variable is undefined, or not set?
Question 3: Can I use my there() function to do this, or will I get the notice just by passing the undefined or unset parameter?
isset and empty are not functions, they are language constructs. As such, they can get away with such things as reading the literal variable name instead of trying to access the value. Any function you define can't do that.
With that in mind, there is no need to test for both. empty is a superset of isset, so you only really need to check for empty.
Your function is almost equivalent to !empty:
if (empty($_SESSION['u'])) {
$_SESSION['u'] = new User();
}
The only difference is: your wrapper function will complain if it's passed a variable that doesn't exist - whereas, as a language construct empty will not (and neither will isset).
Your sub-questions
Why am I not getting the notice on my home development computer?
Probably because you've got error reporting turned off.
Does empty() in general avoid giving a notice if the variable is undefined, or not set?
Yes.
Can I use my there() function to do this, or will I get the notice just by passing the undefined or unset parameter?
You will get notices about undefined variables.
As a way of proving #Kolink 's fine reply really, something which I had not realised ...
echo '<form action="" method=POST>
<input type=text value="" name="string" />
<input type=text value=0 name="int" />
<input type=submit />
</form>';
if( empty($_POST['string'] ) ){
var_dump( $_POST['string']) ;
}
if( empty($_POST['int'] ) ){
var_dump( $_POST['int']) ;
}
if( empty($_POST['something'] ) ){
echo 'something was not set, but you cannot var_dump it without a warning';
}
I realize this is an old thread, and it already has an accepted answer. So, I will not answer the questions again here. But for completeness sake and in the hope that it may help others I am sharing something I use.
Take a look at the following code:
<?php
echo var_dump($_SESSION['something']);
This will give you:
PHP Notice: Undefined variable: _SESSION in - on line 2
NULL
But if you do this:
<?php
echo var_dump(mycheck($_SESSION['something']));
function mycheck( &$var ){ // Note the & so we are passing by reference
return $var;
}
You will get:
NULL
This is because internally the $var variable will get created the instant the function is called. But since the $_SESSION['something'] does not exist, the $var is getting set to null which is then returned. Voila, notice gone. But be aware that the variable $_SESSION['something'] has now been created internally, even though isset($_SESSION['something']) will still return false because isset determines 'if a variable is set and is not NULL'.
PHP manual for isset: http://php.net/manual/en/function.isset.php
When I'm developing websites, I use a very (very) simple system of creating pages:
//$_page is, of course, declared above the loop, just as $needed_modules is.
foreach ($needed_modules as $part)
{
global $_page;
if (file_exists($part)) {
$_page . file_get_contents($part);
} else {
//404
}
}
echo $_page;
Now, the problem is that file_get_contents doesn't return anything: not false, not a string, nada (and the file is not empty).
Execution does go inside the if and $part (which corresponds to a filename with relative path) isn't just set, but it actually points to the file.
Why is it that $_page is empty (as opposed to being set, isset($_page) actually evaluates to TRUE)?
Edit: Error-reporting is on full throttle on my server, and the logs show nothing out of the ordinary.
You are not saving the return value of file_get_contents:
$_page . file_get_contents($part);
I think you meant to say:
$_page .= file_get_contents($part);
You're not doing anything with the returned value. Try this:
$_page .= file_get_contents($part);
The problem
The following code produces this error from the line "print $readerStatus" -
Undefined index: readerStatus
Code
<?php
//Get Cookie Value
if (isset($_COOKIE['readerStatus'])) {
$readerStatus=$_COOKIE['readerStatus'];
} Else {
$readerStatus="Not Set";}
echo "The value of Cookie readerStatus is " . $_COOKIE['readerStatus'];
print $readerStatus;
?>
Background
The goal is simply that if a cookie is set I want to pass the value into a Javascript. My strategy is as follows:
Get the value from the cookie
Set a variable to the value of the cookie
Then use a php echo inside of the Javascript to transfer the value.
It works as expected but Eclipse is giving me the error and so I assume there is something wrong with the above code.
I'd appreciate any pointers on possible sources of the problem.
Thanks
Is this working?
<?php
//Get Cookie Value
if (isset($_COOKIE['readerStatus'])) {
$readerStatus=$_COOKIE['readerStatus'];
} else {
$readerStatus="Not Set";
}
echo ("The value of Cookie readerStatus is ".$readerStatus);
print ($readerStatus);
?>
This is a warning, not an error. However, you can skip the error by using array_key_exists. Generally, I'm not a fan of isset for this kind of checking.
if (array_key_exists('readerStatus', $_COOKIE))
{
$readerStatus=$_COOKIE['readerStatus'];
}
else
{
$readerStatus='Not Set';
}
echo 'The value of Cookie readerStatus is '. $readerStatus;
Some IDEs are less forgiving than the PHP parser itself. That being said, do you get any errors or notices when running the code? Variables in PHP are implicitly declared, so the undefined index message is simply a NOTICE (that can be ignored) regarding the accessing of an array element without it existing first.
If you check it exists prior to accessing it like this, you shouldn't have a problem.
$readerStatus = isset($_COOIKE['readerStatus']) ? $_COOIKE['readerStatus'] : '';
I'm writing a bit of the code and I have parent php script that does include() and includes second script, here is snippet from my second code:
echo ($GLOBALS['key($_REQUEST)']);
I'm trying to grab a key($_REQUEST) from the parent and use it in child, but that doesn't work..
this is when I run script using command line:
mbp:digaweb alexus$ php findItemsByKeywords.php test
PHP Notice: Undefined index: key($_REQUEST) in /Users/alexus/workspace/digaweb/findItemsByKeywords.php on line 3
PHP Stack trace:
PHP 1. {main}() /Users/alexus/workspace/digaweb/findItemsByKeywords.php:0
mbp:digaweb alexus$
i heard that globals isn't recommended way also, but i don't know maybe it's ok...
$_REQUEST is a superglobal and will be directly available inside of any function or script, so you don't need to worry about passing it to the child script. However, PHP won't populate $_REQUEST when used from the command line, unless you're using a configuration option I'm unfamiliar with. You'll need to use the $_SERVER['argv'] array.
Globals are indeed not recommended. You'll have an easier time long-term if you go with what outis suggested. Here's an example:
script1.php:
<?php
$file = $_SERVER['argv'][1]; // 0 is the script's name
require_once ('script2.php');
$result = doSomething ($file);
echo $result;
?>
script2.php:
<?php
function doSomething ($inputfile)
{
$buf = file_get_contents($inputfile);
$buf = strtolower($buf); // counts as something!
return $buf;
}
?>
This example doesn't make use of the key($_REQUEST), but I'm not sure what the purpose of that is so I just went with $_SERVER['argv'].
Based on your comment to my other answer, I think I understand what you're trying to do. You're just trying to pass a variable from one script into another script that's included.
As long as you define a variable before you include the script, it can be used in the included script. For instance:
// script1.php
$foo = 'bar';
include_once('script2.php');
// script2.php
echo $foo; // prints "bar"
echo $_GLOBALS[key($_REQUEST)];
You just need to remove the single quotation marks. It was looking for the literal 'key($_REQUEST)' key, which obviously doesn't exist.
It all depends on what you are trying to do though... what are you trying to do?