?
Solved

php prevent error

Posted on 2014-12-26
17
Medium Priority
?
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
LVL 111

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
 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Fine Tune your automatic Updates for Ubuntu / Debian
This article discusses how to implement server side field validation and display customized error messages to the client.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

762 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