I can successfully tail a message log that my web developer friend setup. Now I have another PHP script that I need to create a log for to help troubleshoot a problem. I copied the code my friend came up with and put it into my new PHP file:
function log_message($message, $type) {
$date = date('m/d/Y h:i:s a', time());
$message = "$date - $type - $message \n";
error_log($message, 3, '/var/log/router');}
Then in various places throughout my code I added lines similar to the following:
log_message("Email to $recipient: was successfully sent.", $info);
I duplicated a log file in /var/log, renamed the file to router and then deleted the existing log information that was in it so that its empty. This log file code works great on the PHP page that my friend created, but I can't get it to work on the page I am troubleshooting.
In the past I simply stored debugging messages in the session, but I'm branching out and trying to make use of log files.
Maybe it is because of the escaping?
function log_message($message, $type) {
$date = date('m/d/Y h:i:s a', time());
$message = $date . " - " . $type . " - " . $message . "\n";
error_log($message, 3, '/var/log/router');
}
Does that work? Hope to help
Related
The question is simple but i will give some background information to hopefully make answering it easier.
So, I am working with the ELGG framework and taking information from a form and the text boxes in it in hopes to print that data to a simple text file.
Unfortunately, for some reason this has been giving me lots of trouble and I cannot seem to figure out why no matter where I look.
Code is as followed
<?php
error_reporting(E_ALL);
//Get the page
$title = get_input('title');
$body = get_input('body');
$date = date("l jS F Y h:i A");
//remove any possible bad text characters
$FileName = $title . ' ' . $date;
//print to file
//get filename and location to save
$folderName = '/Reports/' . $FileName . '.txt';
$ReportContent = "| " . $body . " |";
//write to the file
file_put_contents($folderName, $ReportContent, 0);
//error check to see if the file now exists
if (file_put_contents($folderName, $ReportContent)) {
system_message("Report Created (" . basename($folderName) . ")");
forward(REFERER);
} else {
register_error("Report (" . basename($folderName) . ") was not saved!");
forward(REFERER);
}
So what above SHOULD do is grab the text from the title and body box (which i can confirm it does from the title at least) and then save it under the /reports/ folder (full path for the plugin is Automated_script_view/reports/ if needed). Yet I will always get the register error, and I cannot seem to find why.
I believe it has to do with the declaration of the folder (/reports/), as if I take that part away, it passes and submits, although it doesn't seem to actually save anywhere.
Any and all advice would be very much appreciated!
The Function file_put_contents(file,data,mode,context) returns the number of character written into the file on success, or FALSE on failure. Note the following corrections and your script will work just fine:
1 Your File name has an 'illegal' character ":" coming from the $date part of the string you concatenate to form the filename.
2 Remove file_put_contents($folderName, $ReportContent, 0); Since the function returns an integer, simple use:
if( file_put_contents($folderName, $ReportContent) > 0 ){
//true
}else{
//false
}
EDIT: I've found the source of the problem. It was the webserver, but it was the way that the webserver was processing my requests via my index page. In other words, a foolish mistake on my part. I only discovered this because of the comments in this thread, however, so thank you.
I've recently started taking a PHP class for school, however I can't seem to get any of my PHP to work. We are supposed to upload our projects to a provided website via FTP, so PHP and all necessary pieces should already be setup on the website. For my first project, I was basically instructed to copy/paste a piece from the textbook and upload it. Even this did not work, coming back with a "No input file specified" error. The code for that was:
<?php
//Get current time in a readable format
$currentTime = date( "M j, Y g:i:s a" );
//Display greeting and time
echo "Hello, world! The current time is $currentTime";
?>
I assumed it was something that would fix itself with time and moved on. Just now, I was working on the next exercise, it stated to create a file with the following code:
<?php
$radius = 4;
$diameter = $radius * 2;
$circumference = M_PI * $diameter;
$area = M_PI * pow( $radius, 2 );
echo "This circle has... <br /> ";
echo "A radius of " . $radius . " <br /> ";
echo "A diameter of " . $diameter . " <br /> ";
echo "A circumference of " . $circumference . " <br /> ";
echo "An area of " . $area . " <br /> ";
?>
However when I tried opening this to check and see if it worked, it returned the following:
"; echo "A radius of " . $radius . "
"; echo "A diameter of " . $diameter . "
"; echo "A circumference of " . $circumference . "
"; echo "An area of " . $area . "
"; ? >
This result... doesn't even make sense to me. At all. Even if it was just pasting my lines as plain text they aren't formatted like that. I understand this is probably an entirely basic question to be asking but I'm just starting PHP and none of this makes sense. None of it is explained either.
EDIT: I've been asked for a bit more information, so here is what I can give.
For the first example, I uploaded it to the webserver via FileZilla and when I opened the webpage, the only text was an unformatted "No input file specified." When I hit "View Source Code", the source code also said simply "No input file specified", despite the fact that I definitely uploaded a .php file with code in it to the webserver.
For the second example, it is clear to me that I messed up, as I simply tried opening the file in Google Chrome, not associated with any webserver. I had no idea you couldn't do that, and honestly that seems fairly inefficient, but I guess it can't be helped.
There is yet a third example that I just uploaded to the server now. An actual exercise that is a part of the homework for the class. The file I uploaded had this exact code:
<html>
<body>
<?php
$x = 4;
$x = $x + 1;
$x += 1;
$x++;
echo $x;
?>
</body>
</html>
This file also returns "No input file specified."
Notice how you start getting input right after the first > in your code.
This is a sure sign that PHP isn't processing your file.
Did you name your file something.php? It needs that .php extension, or else custom configuration, or else it won't handle it.
Are others having the same problem? If the files are named *.php then perhaps the PHP module is not enabled on the web server or the web server is somehow otherwise configured incorrectly.
I'm trying to accept a form and write it to a CSV (invisible to the people submitting the form, but I can look at it as a compilation of everyone's entries on the server when I feel like it). Every time someone enters the form, it will become a new line on the CSV. To show that the people are actually submitting, a new tab will pop up with a little "thank you" like message and their submission so they can make sure it's theirs. Yes, I do have a JS form validation that works perfectly, but since that doesn't have a problem I left it out to save space.
Here is my current problem. In Firefox, I just get a blank new tab and nothing changes on my--blank--CSV, which is titled testForm.csv. In Chrome, a new tab opens that contains all the code on my php document, and my CSV stays blank.
Here's the snippet of my HTML:
<html>
<body>
<form name="Involved" method="post" action="postest.php" target="_blank" onsubmit="return validateForm();">
Name: <br><input type="text" name="name" title="Your full name" style="color:#000" placeholder="Enter full name"/>
<br><br>
Email: <br><input type="text" name="email" title="Your email address" style="color:#000" placeholder="Enter email address"/>
<br><br>
How you can help: <br><textarea cols="18" rows="3" name="help" title="Service you want to provide" style="color:#000" placeholder="Please let us know of any ways you may be of assistance"></textarea>
<br><br>
<input type="submit" value="Submit" id=submitbox"/>
</form>
</body>
<html>
Here is postest.php:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$help = $_POST['help'];
$csvData = $name . "," . $email . "," . $help . '\n';
echo "Thank you for your submission! We'll get back to you as soon as we can!";
echo "I'm " . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
$filepointer = fopen('testForm.csv','a');
if ($filepointer){
fwrite($filepointer,$csvData);
fclose($filepointer);
exit();
}
?>
I checked out this question about echoing to see if that was my problem. I asked this question before and nobody seemed to find anything wrong with my code other than the obvious $_POSTEST problem. This page looked like what I was going for, but wasn't. This question kind of had what I was going for but didn't actually have the POST code and the answer was one of the most useless things I've ever read (in a nutshell: "Just do it. It isn't that complicated." and some links to other SO questions, which I followed). They brought me here and here. I put exit(); after fclose() like it seemed to work for the first one (it did nothing). With the second, the user's code was too far removed from the codes I've been looking at for me to change my code over to what he/she was doing. I've been searching a lot, and doing extensive googling, but I'm going to cut my list of research here because nobody wants to read everything; this is just to prove I tried.
Let me know if there's anything else you need; I am a complete php novice and it's probably something very basic that I missed. On the other hand, I'm not seeing any major differences between my code and others' at this point.
Try something like this :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$help = $_POST['help'];
$filepointer = fopen('testForm.csv','a');
fputcsv($filepointer, array($name,$email, $help));
echo "Thank you for your submission! We'll get back to you as soon as we can!";
echo "I'm " . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
?>
This is the error :-
---> $filepointer = fopen('testForm.csv','a');
$fp = fopen('testForm.csv','a');
if ($fp){
fwrite($fp,$csvData);
fclose($fp);
exit();
}
And the real issue is developing without
display_errors = On
log_errors = On
Look for these parameters in the php.ini file, and turn them on, unless you are developing on a live server, in which case, you really should set up a test environment.
and then not looking at the php error log
UPDATE
There was only one line to change actually, here is the complete code.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$help = $_POST['help'];
$csvData = $name . "," . $email . "," . $help . '\n';
echo 'Thank you for your submission! We\'ll get back to you as soon as we can!';
echo '\"I\'m \"' . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
$fp = fopen('testForm.csv','a'); // only line changed
if ($fp){
fwrite($fp,$csvData);
fclose($fp);
exit();
}
?>
Your error is really basic and I am ashamed of you. Your problem is obviously that you have not been using a server, nor do you have a PHP package installed on your computer. When you told your computer target="_blank" and method="post", it knew what you wanted, being HTML. However, not having anything that parsed PHP, it had no idea how to read your code and came up as a blank page in Firefox and a block of code in Chrome.
You, indeed, have no idea what you are doing.
I would like to create a form in html where the user enters name, company, e-mail and country.
When submitting the form, the user will get an e-mail to his/hers e-mail address with a specific link to a .zip file from my server.
The zip file will change name every quarter, so for instance, this quarter it's "download/R2014.1.zip", next is "download/R2014.2.zip".
I think you are creating the email via sendmessage in PHP. When building up the message text, try something like this:
$currentMonth = date("m", time());
$curentQuarter = ceil($currentMonth/3);
$filename = 'R' . date("Y") . '.' . $currentQuarter . '.zip';
$link = 'Download this zip file';
$message = 'Some Email Text. ' . $link . ' Rest of email.'
Hope this helps a bit.
I have built a jquery mobile form that submits to a php file. I want the following php output to also have the option of emailing it to the user.
<?php
$DCity=$_GET["DCity"];
$ACity=$_GET["ACity"];
date_default_timezone_set($DCity);
$DZone = date("Y-m-d h:i:s A");
date_default_timezone_set($ACity);
$AZone = date("Y-m-d h:i:s A");
echo ((strtotime($DCity) - strtotime($ACity))/3600)." hour Timezone Difference</br>";
?>
This will output something like "5 hour Timezone Difference".
Below this php I have a this code which should save the output for an email.
<?php
ob_start();
$DCity=$_GET["DCity"];
$ACity=$_GET["ACity"];
date_default_timezone_set($DCity);
$DZone = date("Y-m-d h:i:s A");
date_default_timezone_set($ACity);
$AZone = date("Y-m-d h:i:s A");
echo ((strtotime($DCity) - strtotime($ACity))/3600)." hour Timezone Difference</br>";
$var=ob_get_flush();
?>
The user then has the option to email this to themselves. If they click the link it takes them to a new form to enter in their email address which posts to a new php that contains this code.
<?php
$to = $_GET["email"];
$subject = "Your Personalized JetLag Pilot Plan";
require 'OriginalOutput.php';
$body = $var;
mail($to,$subject,$body);
echo "Mail sent to $to";
?>
The original php output is working correctly. However whenever I send an email that should contain the same output, it always says "0 hour Timezone Difference." The email form is working correctly just not carrying the OriginalOutput.php. Anyone know where my mistake is?