Embedding PHP inside PHP - php

I have recently put up a server, and I am making changes gradually. One of the changes is the contents of a php page. I commonly use a php "shell" (just a textarea and the php just evals what is written). I have made some changes and want to use
fwrite($file, "<?php php here ?>");
but i am getting an error. I believe I know the problem, and it is because i have nested php like
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval($_POST['phptorun']);
?>
");
fclose($a);
?>
Is there any way I can get it to just put that php code into the file php.php?
The error i was getting is:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /var/www/html/Tyler/replace.php on line 5
Edit:
I forgot to say a couple of things. The server is a LAMP (Linux Apache MySQL PHP), running on my Raspberry Pi 2, model B. If you want to visit my server, it is at 71.204.114.18

Try to escape the $ symbol:
<?php
$a = fopen('../php.php', 'w');
fwrite($a, "
<?php
eval(\$_POST['phptorun']);
?>
");
fclose($a);
?>

Related

XAMPP PHP Named Arguments gives " syntax error, unexpected ':', expecting ')'"

In an include file I declare a function
function patch_163_output_row_header(
string $title,
string $label,
$help,
bool $read_only=false,
bool $required=false,
string $label_cell
)
and when I compile this using the XAMPP PHP compiler (8.1.6 as far as I can tell) it is OK
In another file I try to use the function
patch_163_output_row_header(
title:$title,
label:$label,
help:$help,
read_only:$read_only,
required:$required,
label_cell:$label_cell
);
when I try to compile I get the error mentioned above on the"title" line.
I can see no obvious issues - title does not seem to be a reserved word?
The strange thing then is that these two files are part of a website and when they are executed by the same XAMPP stack (presumably using the same PHP compiler?) there are no errors reported and they work.

echo -e content of file fetched using foreach

Forgive me, I'm not a programmer.
I am trying to include the last lines of a log file on a PHP page with two line breaks after each line read. Using responses from other questions, I came up with the following:
$file = "error.log";
foreach(file($file) as $line) {
echo -e $line. "\n\n";
}
When I execute this on the command line using php file.php, it is displayed correctly with the line breaks, however, on a webpage, it ignores the line breaks and prints a dirty output.
I tried to use echo -e in place of echo, but that doesn't even present the dirty output, but instead throws an error:
PHP Parse error: syntax error, unexpected '$line' (T_VARIABLE), expecting ',' or ';' in /var/www/err.php on line 12
I'm assuming echo -e isn't allowed within the foreach function? What am I doing wrong and how can I achieve the desired results?
echo in PHP is different than the echo command line in linux. You can't pass arguments to echo in PHP like that.
try :
echo nl2br($line . PHP_EOL);
Or if you only care about the browser
echo $line . "<br>";

Unexplained syntax error with logical OR ( || )

I'm working with NetBeans for Mac, and I'm running CakePHP (though I don't think the framework has anything to do with it) in a shared hosting in Linux. This is not a big issue, but it is frustrating.
I wonder why I can't simply do this:
if($this->Session->read('User.value1') || $this->Session->read('User.value2')){
...
}
The error message I get is:
Error: syntax error, unexpected '$this' (T_VARIABLE)
Why is there a syntax error? I can't see it.
I can do this with no problems:
if($this->Session->read('value1')){
...
}
I can also do this with no problems (no whitespace around ||):
if($this->Session->read('User.value1')||$this->Session->read('User.value2')){
...
}
But if I put spaces around the || operator, it stops working. Or rather — and this is the most confusing part — sometimes it stops working when I put spaces around the || operator, and sometimes it doesn't.
I thought this might be a bug in Netbeans 7.4, but when I ignored the warning from NetBeans and tried to run the code anyway, PHP gave me the same error.
What is happening here?
I'm working With NetBeans for MAC
When is a space not a space?
When it's a non-breaking space!
The intention is:
" || "
207C7C20 (hex)
But what is actually in the source file is almost certainly:
" || "
207C7CA0 (hex)
(on stack overflow it won't be but I bet it is in the source file).
With a mac the problem is (using my own keyboard layout, but I am assuming it's similar in your case):
"|" = alt + 1
" " = alt + space (accidental)
So typing away, with the sequence " || " it's very easy for the alt key to still be depressed when the space bar is pressed and: voilà you get unexpected "wat" syntax errors which at face value make no sense - until you realize what the problem is.
Example:
-> cat foo.php
<?php
$foo = "x";
if (true || $foo) {
}
-> php -l foo.php
Parse error: syntax error, unexpected '$foo' (T_VARIABLE) in foo.php on line 4
Errors parsing foo.php

PHP is calling error on code that in not being run?

I was troubleshooting some code and I took it out of the execution path so that I could look in other places.
PHP continued to call out the code as an error despite it not being executed. It is in a function that is not called.
Is this expected behavior?
// not in execution path
$temp1 = $this->hash;
$temp = array('1', $temp1); // this is line 121
$this->database_object->_pdoQuery('none', 'update_picture', $temp );
syntax error, unexpected T_LNUMBER, expecting ']' in
/home/foo/public_html/dev/foo/source/class.MAppPicture.php
on line 121
The entire file has to be parsed prior at the time of access or include. This is generating an error because it is a parse time error and php can't understand the file as a whole because of the syntax error.

What php configuration variable disallows concatenated array indexes?

E.G.
if(!isset($am_states[$lot.'_-40C'])){
or
$am_states[$temp."_".$states[$i]['temperature']] = $states[$i]['temperature'];
Whenever I have arrays with concatenated string as array-keys php returns an error:
Parse error: syntax error, unexpected '.', expecting ']'
So I am assuming something is wrong with the server configuration although I am sure i changed something on my local configuration.
Last time i changed the configuration was when i setup my apache/mysql/php installation
that is PHP Version 5.3.1,Apache/2.2.14,MYSQL5.1.41 (default from xampp1.7.3)
so I was using this syntaxes 7 months ago and they were working properly. It just now that they produce errors.
Anyone can help?
Are you positive about the PHP version you're using? The following test (using PHP 5.3.6 (cli)) works without issue. Perhaps you could post a more complete example?
#!/usr/bin/env php
<?php
$states = array(
array('temperature' => 40),
array('temperature' => 50),
array('temperature' => 60)
);
$temp = 'test';
$i = 2;
$am_states[$temp . "_" . $states[$i]['temperature']] = $states[$i]['temperature'];
var_dump($am_states);
The output of this script is:
array(1) {
["test_60"]=>
int(60)
}

Categories