Solved

php prevent error

Posted on 2014-12-26
17
196 Views
Last Modified: 2014-12-26
I have this small snip of code which checks for the existence of a file. If the file is missing, it should not throw an error in the logs but gracefully exit. Wondering if someone could help me with this, by adding code which will prevent the apache error.

        elseif ( $_POST['function'] === 'fwfinal' ) {
                $file = $_POST['filename'];
                $fn = "logfiles/$file";
                if (@file_exists($fn)) {
                if ( $fn ) {
                    unlink ($fn);
                    return "Success";
                }
       }
}
0
Comment
Question by:projects
17 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40519003
Remove this condition

if ( $fn ) {

And the @ symbol

What is the error you are getting?
0
 

Author Comment

by:projects
ID: 40519118
Well, the client is looking for a file in a subdirectory.
When the file is not there, is it creating an error in the Apache logs which is uselessly filling the logs.

The error is;

"HEAD /subdir1/logfiles/fxrr44.log HTTP/1.1" 404 -

The client knows and is looking for a specific log file name.

I will wait to hear back from you before changing anything. Also, can you just edit the code and paste the full output just to make sure I don't mess something up.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 40519131
I think you are going have to do this from with the Apache conf files

SetEnvIf Request_URI "^/subdir1/logfiles/*.log$" dontlog

Open in new window


http://www.serverwatch.com/tutorials/article.php/10825_3376671_2/Advanced-Logging-Techniques-With-Apache.htm

        elseif ( $_POST['function'] === 'fwfinal' ) {
                 $file = $_POST['filename'];
                 $fn = "logfiles/$file";
                 if (file_exists($fn)) {
                     unlink ($fn);
                     return "Success";
        }
 }

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40519151
In PHP you can use the @ notation to suppress PHP messages about errors.  This is a bit risky -- it can cause PHP scripts to fail silently without any PHP-evident error information at all.  I don't know how to suppress the information in the Apache logs, so I am going to edit your question topic areas in the hope that someone with Apache knowledge can help.  But in PHP I think you can find out if a file exists and skip over the process of deleting it when it does not exist with something like this:

$file = $_POST['filename'];
$fn   = "logfiles/$file";
if (file_exists($fn)) 
{
    unlink ($fn);
}
return 'Success!';

Open in new window

0
 

Author Comment

by:projects
ID: 40519169
Hi Ray,

I've posted about this before but the details are that the connections allowed to this directory are all authenticated. When the file doesn't exit, it is generating an error 404 which is filling my logs making it hard to see actual errors.

This is not really an error in my case, it simply means that there was no file waiting this time so I would like to prevent the error.

Once the client has downloaded the file, it should be deleted.

I have tried both suggestions and neither one works, the error still shows up in the error log.
Note also that this is an ssl connection so I've added the apache directive in the appropriate section of this particular virtual host.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40519174
Did you try the SetEnvIf, that should disable any logging for that directory

SetEnvIf Request_URI "^/subdir1/logfiles/$" dontlog

Open in new window

0
 

Author Comment

by:projects
ID: 40519187
Yes, I added this to httpd.conf in the appropriate section.
This is the URL and not the server path right? Just double checking.

Also, I had an original question which was the same problem when the code was different.

http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28477747.html
0
 
LVL 58

Expert Comment

by:Gary
ID: 40519191
And restarted Apache?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:projects
ID: 40519217
Yes.

I found that if I took the path out, then it works. I would prefer having the full path but this works.

            LogLevel warn
            SetEnvIf Request_URI \.log dontlog
            ErrorLog /var/www/hosts/somehost/logs/ssl_error_log
#           TransferLog /var/www/hosts/somehost/logs/ssl_access_log
            CustomLog /var/www/hosts/somehost/logs/ssl_access_log combined env=!dontlog

I would certainly prefer eliminating the error through php however.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40519221
You can't do it in PHP because this is a server level message and not a PHP message
0
 

Author Comment

by:projects
ID: 40519223
In a previous question, it was done on the php side.
When the client would look for its file, if it was there, it downloaded it then deleted it.
If the file was not there, it would not generate an error in the logs.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40519225
There is a difference between PHP error logs and Apache logs
If a file is not found (404) then there is no PHP executed to prevent the logging - this is done by Apache before PHP even comes into the equation.
0
 

Author Comment

by:projects
ID: 40519226
Yes, I understand that. What I am saying that is in a previous question about the same problem, someone offered a solution which updated my php code so that there was no error being generated when the file was not there.

At least, I believe that was the case but I don't know now, it's been too long.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40519229
I'm not sure what http:#a40519151 has to do with the answer since it just copied my amended version of your code from an hour earlier

I will look into it a bit more, but I don't see how.
0
 

Author Comment

by:projects
ID: 40519231
Maybe I posted the wrong question. I'm in the middle of a number of things so would have to look closer.
I'm not worried about it, the Apache directive will work fine from now on for this purpose and I've updated the code based on Ray's input also.
0
 
LVL 30

Expert Comment

by:Kerem ERSOY
ID: 40519238
Can you check error_reporting function?
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now