View Full Version : Creating a gdbm dba file
cmahnken
06-06-2001, 12:47 AM
Multi-part Question for the linux generation:
I have a small quantity of info to track in a db. there will be three or four records in a single table with only two columns. one of those records will be modified every time there is a hit on the page. Seems perfect for PHPs dba functions, also easy with mysql.
Question 1: Which would be easier on the servers. modifying a dba (gdbm) db, or a MySQL db?
Question 2: If Q1 was dba/gdbm, then how can I create the file on my site (safe-mode prevents file creation). I assume I telnet in and create the file that way, but exactly what would the command be to do that?
Thanks for any help!
------------------
Chris Mahnken
Since there are only 6-8 pieces of information total, I would think it would be much faster and efficient just to write them to one or more files. :)
Rich
cmahnken
06-06-2001, 01:09 AM
Indeed. That's basically what the gdbm file would be. Calling it a database is stretching the matter :) more like an extended fast INI file. But while it's supported by our php configuration, creating of files from PHP is not. So how do I create it?
Would you need to create the file on the fly?[nbsp][nbsp]If not, you can simply create the file (blank initially, if you want) and make it writeable, and that should avoid safe-mode restrictions.
Dan
Terra
06-06-2001, 03:14 AM
Another way to work with the safe_mode environment:
1) Create a mode 777 directory
2) Create the dbm hash via dba_open or dbmopen into that 777 directory
2) right after creation, make sure that you either set proper umask at top of script or chmod the new dbm to 666...
Just takes a little bit of creativity...
--
Terra
--Security was never really designed to be straightforward--
FutureQuest
GregJ
10-17-2001, 03:51 AM
I had been trying to create a dba_ file using dba_open. I noticed from this thread that that is not possible, so I tried changing the directory to 777 (just for the duration of the create) - That didn't work.
I tried FTPing an empty file (zero bytes). My script can detect the existence (or absence), but dba_open still fails. I changed the file to 777, No Good either.
I even removed the .htaccess file - NG.
I'm a novice PHP programmer so I'm probably doing something stupid. Any help or suggestions from anybody will be appreciated. I'll send the scripts to anybody who wants to look.
That leads me to the next question: Where are the error messages I should get when I make a syntax error? It is a real pain getting only a blank screen back.
GregJ
--Stuck deep in the heart of Texas
<edit - Must be too tired to type streight... * 2>
Terra
10-17-2001, 05:09 AM
Where are the error messages I should get when I make a syntax error? It is a real pain getting only a blank screen back.
http://www.aota.net/forums/showthread.php?s=&threadid=5674&highlight=php+error+display
I just ran a test on our servers, and it worked properly using the 'gdbm' database library... Make sure that your path is correct, and the directory is mode 777...
The test was done "by-the-book":
http://www.virginvinyl.net/dba/
Most likely, you have a syntax error in your program somewhere...
--
Terra
sysAdmin
FutureQuest
GregJ
10-17-2001, 05:15 AM
OK, I can now open the file for write. I found a typo on a variable when I found the php_errors file.
I have had to give it World Writeable. Is this correct? I was thinking it should be something like r/w to owner, r or r/w to group, and nothing to the world. I would prefer to see read and update access restricted to myself and 'my' web server.
I've opened the database with dba_open and am trying to use that handle with flock. Perhaps this is a non-sequitir and I need to use a handle returned from fopen.
What locking facilities are available with the dba_ functionality?
If there isn't anything, how does one serialize access? Should I use the semaphore functions?
Would something like this work?
if (file_exists($fname)) {
$fh=fopen($fname);
flock($fh, LOCK_EX);
$dbh=dba_open(...);
}
GregJ
--I was talking to a shepherd and he didn't know about exclusive flocks
GregJ
10-17-2001, 05:20 AM
Hi Terra, Sorry I didn't see your post. I was adding a follow-on to my original note. Yeah, I found the typo as mentioned in the second post.
GregJ
Terra
10-17-2001, 05:28 AM
The modes must be:
dir == 777
file == 666
since the 'apache' user must be able to create the db file... Since _you_ own the dir, you are then allowed to unlink the file...
As far as locking, you will need to do some digging on that as I'm pretty sure that 'gdbm' doesn't support multiple writers, and I don't _think_ that 'db2' does either... Multiple readers should be fine though, and you should only *lock* when you go to write to the db...
flock EX
dba_replace()
dba_sync()
flock UN
I encourage you to read the PHP archives to find solid definitive answers to the serialization questions... I would have, but don't have the time to go on an expedition at the moment...
--
Terra
sysAdmin
FutureQuest
GregJ
10-17-2001, 05:54 AM
I placed the line
php_flag display_errors On
in my .htaccess file and Presto! Messages on screen!
It's nice that it can be set by directory via the .htaccess. That way I can make my test directory blabbermouth.
I was searching earlier today for information on the .htaccess file. Is there a discussion anywhere on what we can put in that file? I see bits and pieces when I look around, but I haven't found anything that gives me a list of possible options with anything like an explanation.
Terra, Just as I was about to post this, I see you added an entry.
666, ok. I was under the impression that the server ran as 'me' when processing from my site.
I will scrounge around the archives to see what I can dig up. If anyone else has experience with this, or knows where to find a discussion of this please let me know.
This particular database will be written to by everyone accessing certain functions, but the update cycle on it will be very short and not a whole lot of traffic in any event.
Thanks,
GregJ
--I never getting worry about scramfilesbled. Who locks? needs
GregJ
--The screen ain't blank anymore
Terra
10-17-2001, 06:09 AM
I never getting worry about scramfilesbled. Who locks? needs
ROTFLMAO!!!
--
Terra
--Worthy competition some I have found looks like, yes believe I do so--
FutureQuest
GregJ
10-17-2001, 02:18 PM
Terra, Thanks for the complement. Holey undeserved - everyone gets luck once in a while.
Well, I can inform you that the idea
if (file_exists($fname)) {
$fh=fopen($fname);
flock($fh, LOCK_EX);
$dbh=dba_open(...);
}
only generated a lockout.
It turns out that the dba_open locks the database as appropriate depending upon the mode passed.
I tested this by creating snippet to open and then sleep for a short time while I re-ran the script in another window.
The dba_open will fail (unfortunately I can't find anyway to get a reason-code from the proces) if it is locked out by another process. Because I don't know the reason for the open failure, I simply retry a number of times waiting about a second between tries. If I run out of retries, then I post a failure message.
Hope this helps someone
GregJ
--I know the info is out there, some day I'll find it
vBulletin® v3.6.8, Copyright ©2000-2009, Jelsoft Enterprises Ltd.