PHP Variables not existing and forms

By , Thursday 31st July 2008 10:36 am

I came across something I wasn’t aware of today that is very handy…. the error control operator ‘@’ in PHP.

So for some background to how I came across this problem, in my web applications I tend to use the same form to insert data into a mySQL database. If I have an some sort of ‘id’ number set via a GET variable then that triggers a load from the database, otherwise I’m adding new data.

In my form elements I often set the value of the form field to something like <?=$data[‘fieldValue’];?> this works fine when loading data but when I’m inserting new data I get the form filled in as follows (note on my login or when I’m testing I have error reporting set to E_ALL so I can make sure everything is running correctly):

Notice: Undefined variable: data in /home/public_html/dataEdit.php on line 69

So what I eventually ended up doing was writing longer (uglier) code which said, <? if (isset($data[‘fieldValue’])) { echo $data[‘fieldValue’]; } ?>. Not exactly great

Then I stumbled across the error control operator in PHP, by simply placing an ‘@’ symbol before an operator (basically anything that can return a result) the above problem is solved. For example I can now make my for value parameter <?=@$data[‘fieldValue’];?>. If the variable is not set (i.e. an error would be generated) then the error is suppressed.

Note: Using the ‘@’ symbol can supress major errors and hide the fact that part of your site isn’t running correctly, so be careful!

Moving Server, Safe Mode, and File Ownership…

By , Tuesday 29th July 2008 3:42 pm

I recently moved some of the web applications that I’ve written over to a new dedicated server with UKFast. The new server I had with my previous terrible web host, was setup in a much less secure way (PHP Safe Mode”>safe_mode off for example) than the new one so I was finding that several scripts weren’t working.

One of the major annoyances was php file uploads used to put property pictures onto the site. With error reporting switched to maximum and Display Errors switched on in my php.ini file I was able to see the full list of errors. One of the major complaints stated something along the lines of…

PHP Warning:  Unknown(): SAFE MODE Restriction in effect.  The script whose uid is 1 is not allowed to access /path/to/script.php owned by uid 26658 in Unknown on line 0;

Many of the directories and image files were created by the old site and so file ownership was a major problem. Technical support suggested turning off safe mode as a short term fix and then working around it in the longer term. I wasn’t quite happy with this solution so spent some time thinking through the problem.

I knew that you could change the ownership of a file by logging in as root via SSH and running the following commands…

<>chown user:group filename (also see chgrp)
(You can use the -R switch to chown all the files in your site)

I started by setting the file and group ownership of the files on my site to 26658 (if we’re using the above example!). This seemed to sort the problem until that is I tried to FTP files to and from the site, resulting in a permission denied and/or file not found errors.

So after playing around with several uid’s I had a cup of tea and thought through the problems. For me to upload files via FTP then the owner of the file is going to have to match the FTP username. So I set the owner of the file as the username I used to log in via FTP. Secondly, the webserver is going to want to access the files so I set the group ownership as ‘apache‘.

This didn’t quite work, but setting the /tmp directory ownership to nobody (chown nobody /tmp) seems to have fixed the problems :)

Panorama Theme by Themocracy

1 visitors online now
0 guests, 1 bots, 0 members
Max visitors today: 16 at 08:14 am UTC
This month: 17 at 19-09-2017 09:35 pm UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC