file_get_contents - "Use of undefined constant code - assumed 'code'" - php

I'm trying to add a basic new-grabber for a site I'm making and cannot figure out for the life of me what is causing this error. The file I'm grabbing is a plain-text file, completely accessible.
I've seen it posted before and the OP is calling something like:
$var = $data[str] instead of $var = $data['src']
But I am not calling anything with "code" in the name.
I receive this error upon running my code:
HTTP request failed. Error 8 on line 123: Use of undefined constant
code - assumed 'code' in file /usr/local/lib/php/head.php
Here is my entire file below:
<?
$e_news = file_get_contents("http://cinemattson.com/templates/flickfeed/news.txt");
if (!$e_news === true) {
$error = error_get_last();
echo "HTTP request failed. Error " . $error['type'] . " on line " . $error['line'] . ": " . $error['message'] . " in file " . $error['file'] . "<br>";
} else {
echo "Everything went better than expected";
}
if ($e_news === true) {
$news = explode("|", $e_news);?>
<h4>News - <? echo (!empty($news) ? $news[1] : "v0.0.1");?> <small><? echo (!empty($news) ? $news[0] : "5/22/2016");?></small></h4>
<p><? echo (!empty($news) ? $news[2] : "Loading news failed, or there is currently no news.");?></p>
<?
} else {
echo "<h4>News failed to load</h4>";
}
?>
Do you guys know what I'm missing or doing wrong here?

As already suggested by #John Stirling, "the issue is elsewhere".
More precisely the reported Error 8 on line 123... etc is related to an error that happened previously, elsewhere.
And your current code is responsible to make this error appear now because you wrote:
$e_news = file_get_contents("http://cinemattson.com/templates/flickfeed/news.txt");
if (!$e_news === true) {
$error = error_get_last();
This way, the following happens:
Each time file_get_contents() is successfull, $e_news gets its content.
Then $e_news === true is FALSE (even if this content is empty, because you used ===), and if (!$e_news === true) is always TRUE.
So there is no error now, and your error_get_last() gets the trace of the last error that previously happened, elsewhere...
In fact, for your code to work as expected you should rather do something like this:
$e_news = file_get_contents("http://cinemattson.com/templates/flickfeed/news.txt");
if ($e_news === false) {
$error = error_get_last();
echo "HTTP request failed. Error " . $error['type'] . " on line " . $error['line'] . ": " . $error['message'] . " in file " . $error['file'] . "<br>";
echo "<h4>News failed to load</h4>";
} else {
echo "Everything went better than expected";
$news = explode("|", $e_news);?>
<h4>News - <? echo (!empty($news) ? $news[1] : "v0.0.1");?> <small><? echo (!empty($news) ? $news[0] : "5/22/2016");?></small></h4>
<p><? echo (!empty($news) ? $news[2] : "Loading news failed, or there is currently no news.");?></p>
<?
}

Related

What is the scope of error_get_last() in PHP?

I've been using error_get_last() in combination with register_shutdown_function() to create an error report for some of my scripts. It's been working fine for the most part.
Every now and then, though, the error report will show an error from a different script.
I'm not quite sure how that can happen. Can anyone explain?
I'm wondering if error_get_last() in one script should be able to show errors from different scripts and if that's the case, under what circumstances would that happen?
My only guess is that the error is saved for a short time so that a script that finishes execution at almost the same time might catch another script's errors. Could that be it?
This is the code I use and have included in many different PHP files:
function send_error_report($extra) {
global $conn;
$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' . $_SERVER['HTTP_HOST'];
$url = $base_url . $_SERVER["REQUEST_URI"];
$error = error_get_last();
if (isset($error)) {
$error_str = "Type: " . $error['type'] . " | Message: " . $error['message'] . " | File: " . $error['file'] . " | Line: " . $error['line'];
$sql = "INSERT INTO `error_report` (`script`, `error`) VALUES ('$url', '" . $error_str . " - " . $extra . "');";
$conn->query($sql);
}
}
register_shutdown_function('send_error_report', '');

php error logging: replacement for deprecated errcontext in error_handler in 7.2.0

Version 7.2.0 deprecated an highly useful bit of information for debugging in error_handler: the errcontext (the 5th parameter) values, which gave the value of variables in the scope of the error. Without that I can get only the file, line and error message, but that doesn't help if the error is a result of the actual values being processed. So the question is: "is there a way to get the values of the variables in a post 7.2.0 world?"
For background, I am using the Larry Ullman (famous php author) custom error handler, but I can no longer use the 5th parameter ($e_vars), below.
======
function my_error_handler ($e_number, $e_message, $e_file, $e_line, $e_vars) {
global $live, $email_errors;
$message = "An error occurred in script '$e_file' on line $e_line: \n<br />$e_message\n<br />";
$message .= "Date/Time: " . date('n-j-Y H:i:s') . "<br />";
$message .= "<pre>" . print_r ($e_vars, 1) . "</pre><br />";
if ($live) { // Don't show the specific error.
error_log ($message, 1, $email_errors,"From:errors#domain.com"); // Send email.
// Only print an error message if the error isn't a notice.
if ($e_number != E_NOTICE) {
echo '<div id="Error">A system error occurred. We apologize for the inconvenience.</div><br />';
}
} else { // Development (print the error).
echo '<div id="Error">' . $message . '</div><br />';
}
}
set_error_handler ('my_error_handler');
======

PHP - Login via form and grab cookie

I'm trying to login to the following URL: pitangui.amazon.com
I have tried using cURL as well as libraries like https://barebonescms.com/documentation/ultimate_web_scraper_toolkit/
However, I'm getting the following error using the webscraper via Amazon:
This is my PHP code:
<?php
require_once "support/http.php";
require_once "support/web_browser.php";
require_once "support/simple_html_dom.php";
$url = "https://pitangui.amazon.com";
$web = new WebBrowser(array("extractforms" => true));
$result = $web->Process($url);
if (!$result["success"]) echo "Error retrieving URL. " . $result["error"] . "\n";
else if ($result["response"]["code"] != 200) echo "Error retrieving URL. Server returned: " . $result["response"]["code"] . " " . $result["response"]["meaning"] . "\n";
else
{
$form = $result["forms"][0];
$form->SetFormValue("email", "myemail#gmail.com");
$form->SetFormValue("password", "mypass");
$result2 = $form->GenerateFormRequest("signIn");
$result = $web->Process($result2["url"], "auto", $result2["options"]);
if (!$result["success"]) echo "Error retrieving URL. " . $result["error"] . "\n";
else if ($result["response"]["code"] != 200) echo "Error retrieving URL. Server returned: " . $result["response"]["code"] . " " . $result["response"]["meaning"] . "\n";
else
{
// Do something with the results page here...
print_r($result);
}
}
?>
I'm first trying to get the login working, then I will grab the cookie via $_SERVER['Cookie']
add
$form->SetFormValue("create","0");

Printing validation message on the same page in PHP

Hi I have this function in handling upload errors.
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL=\"$location\"");
echo 'Error :' . $error . ' Please correct before proceeding.';
exit;
}
Since this is echo's the message, you know the message shows not in the same page and you need to hit browser back to return to the original content page.
What I wanted is the validation messages to be shown on the same page, so I tried changing the code to following.
function error($error)
{
$ErrMsg = 'Error :' . $error . ' Please correct before proceeding.';
}
And later in the form in a table I have something like following to call the massage.
<td><?php if(!empty($ErrMsg)) echo $ErrMsg; ?></td>
However this method doesn't seems to be giving me the solution I need, which is to print the validation message on the same page.
Can anyone help?
thanks.
You could use ajax to call the error function and output the message on the page.
Or you could set a $_POST, $_GET, or $_SESSION variable to pass the error message onto the next page.
Session example:
function error($error)
{
$_SESSION['ErrMsg'] = 'Error :' . $error . ' Please correct before proceeding.';
}
And later...
<td>
<?php if (!empty($_SESSION['ErrMsg'])) {
echo $_SESSION['ErrMsg'];
}?>
</td>
If the form posts to the same page it's on, you could try something like this:
function uploadValidation()
{
$errorReason = '';
// do validation here
// if something goes wrong $errorReason = 'reason for error'
if (!empty($errorReason)) {
return 'Error :' . $errorReason . ' Please correct before proceeding.';
} else {
// success stuff
// maybe return thanks message or redirect somewhere else?
return 'You upload real good!';
}
}
$validationMessage = uploadValidation();
?>
And later in the form in a table I have something like following to call the massage.
<td><?= $validationMessage ?></td>
Add { after your if... codes and just before echo and a closing } after your echo... codes.
It's actually pretty hard to tell what you are trying to achieve based on your description. But try this:
<?php
function error($error) {
return 'Error :' . $error . ' Please correct before proceeding.';
}
?>
And then:
<td><?php echo error('YOUR ERROR CODE') ?></td>

PHP code is displayed in user's browser

I have a php script like this :
<?php
$confirmationCode = trim($_GET['confcode']);
$_SERVER['REMOTE_ADDR'] = 'xxx.xxx.xxx.xxx';
$emailLogId = 1;
if ($_SERVER['REMOTE_ADDR']=='xxx.xxx.xxx.xxx') {
// print '<pre>' .'xxxxx' . $emailLogId . '###'; //exit ;
}
if( is_numeric($emailLogId)) {
if ($_SERVER['REMOTE_ADDR']=='xxx.xxx.xxx.xxx') {
// print '<pre>yyy' . $_GET['emaillog_id'] . 'yyyxxxxxx ' . $emailLogId; print_r ($row) ; exit ;
}
//$osDB->query('UPDATE ! SET clicktime=? WHERE id=?', array('email_logs', time(), $emailLogId));
} else {
if ($_SERVER['REMOTE_ADDR']=='xxx.xxx.xxx.xxx') {
// print '<pre>zzz' . $_GET['emaillog_id'] . 'yyyxxxxxx ' . $emailLogId; print_r ($row) ; exit ;
}
}
?>
It is running on my server. Actually some people are complaining that they are seeing the source code of this script( pasted below ) on their browser and they send me snap shot of this issue:
' .'xxxxx' . $emailLogId . '###'; //exit ;
}
if( is_numeric($emailLogId)) {
if ($_SERVER['REMOTE_ADDR']=='xxx.xxx.xxx.xxx') {
// print '<pre>yyy' . $_GET['emaillog_id'] . 'yyyxxxxxx ' . $emailLogId; print_r ($row) ; exit ;
}
//$osDB->query('UPDATE ! SET clicktime=? WHERE id=?', array('email_logs', time(), $emailLogId));
} else {
if ($_SERVER['REMOTE_ADDR']=='xxx.xxx.xxx.xxx') {
// print '<pre>zzz' . $_GET['emaillog_id'] . 'yyyxxxxxx ' . $emailLogId; print_r ($row) ; exit ;
}
}
?>
Actually I am really confused because I am not able to reproduce this problem, but 3-4 people are complaining about same the thing.
Do you have any idea what is the issue?
Yes, similar thing happened with me too.
2 things:
. Apache configuration.
Make sure php engine is ON. If you cannot access your apache configuration file then, add this in your .htaccess:
php_flag engine on
. CDN.
If you are using any Cloud Distribution Network, it is time for you to ask them to purge your existing cache and re-load the new one.
Browser will display PHP source code ONLY AND ONLY if apache configuration is going wrong.
Hope that helps.
EDIT:
After reading Sabin's comment, I gave a second look at the code.
Problem is, he has ASSIGNED the value to $_SERVER['REMOTE_ADDR'] (line 3)
Here is how it should be:
<?php
$confirmationCode = trim($_GET['confcode']);
$ip = 'xxx.xxx.xxx.xxx';
$emailLogId = 1;
//Whatever conditions.
if ($_SERVER['REMOTE_ADDR'] == 'xxx.xxx.xxx.xxx') {
// print '<pre>' .'xxxxx' . $emailLogId . '###'; exit ;
}
if( is_numeric($emailLogId)) {
if ($_SERVER['REMOTE_ADDR'] == 'xxx.xxx.xxx.xxx') {
// print '<pre>yyy' . $_GET['emaillog_id'] . 'yyyxxxxxx ' . $emailLogId; print_r ($row) ; exit ;
}
//$osDB->query('UPDATE ! SET clicktime=? WHERE id=?', array('email_logs', time(), $emailLogId));
} else {
if ($_SERVER['REMOTE_ADDR']=='xxx.xxx.xxx.xxx') {
// print '<pre>zzz' . $_GET['emaillog_id'] . 'yyyxxxxxx ' . $emailLogId; print_r ($row) ; exit ;
}
}
?>
However, echo-ing the source code cannot be due to this. I would ask you to put FULL FILE so that we could see if you are missing a closing single quote!
It sounds like PHP is not working at all. The only reason you are not seeing the first part, is because your browsers is parsing it as if it were an HTML tag.
And Please try to print phpinfo() once,
please check for the below link, for more details
PHP code displayed in browser

Categories