Hi, I have a slight problem with opening a (dedi config) file for read access. The code does work, but when OpenFile () fails it takes aaaaages (60 sec execution timeout almost) for the script to react to the header () redirection. Also, in task manager it shows PHP taking 99% of CPU cycles. Any idea why this, and why OpenFile () fails with a existing URL type path (allow_url_fopen is On)?
I would like to help you, but Im noob my self and having really hard to read your code.
I always use else statement to keep code readable.
I think your code is not returning the FALSE as it should and that could do that the main code continues without the header.
Try to write it with else commands and but in some debug to see what the code is doing.
Yeah I think that's the problem, but had the same thing happen when I rewrote the funct with the else blocks. Also, it doesn't really explain why it fails to open perfectly working URLs (e.g. http://localhost/dedi/setup.cfg, which opens when I put it in the browser's address bar)
Always check incoming data that it is what it is supposed to be and that it doesn't cointain any known dangerous characters and that the character encoding is set.
But if that is not going to be in the internets, only on your localhost that isn't accessible from the internets, then it doesn't matter of course...
It's definitely not the code missing some else's, the fact that PHP gets stuck suggests some kind of lengthy timeout for fopen, maybe related to using an url to open the file, but it still shouldn't consume any noticeable amount of cpu.
Why do you want to open the file through an url? Why do you want to manually read an ascii text file as binary? Why are you not using the apache php module?
I'm not a programmer, and have never been taught any coding as such (apart from one uni module in PHP).
I don't rly know how else to read the file as easily (see code below). All I really want is to be able to loop thru a server config file, store each key=value pair in an array (or object), and put them in a nice form so that I can easily modify the settings and save it on the system that's running the script.
Why is get_file_contents () better than using it as a resource? Also, would it be better then to write the entire config from a string to a file with file_put_contents ()? Currently I open (or create) the file for write access as a resource and fwrite each /key=value pair into it and close it.
This class is the config template which is used hold the settings when reading from file, URL or form (when saving to file). I don't have the actual code in front of me, so it's off my head.
<?php class LFSConfig extends DCFramework { var $host = "Host Name"; // ... all server options
function Parse ($file) // $file = fopen ("http://localhost/foo/bar.cfg", "rb"); from OpenFile()
foreach ($line as $key => $value) { $line = fgets ($file); if ($key != "") { // iggy blank lines had some probs with if (!$key) { if (!preg_match ('/^\/\//', $line) && preg_match ('^/\//', $line)) { // only select lines starting with '/') $this->$key = $value; } } } } // end function } ?>
I want to open it as binary because of newline issues. I thought there might be problems of compatibility between filesystems that use different newlines (\n, \r\n) and reading in text mode. Am I wrong?
I am not using the apache php module because I cannot get the PHP module to talk to MySQL. I'd prefer to use the module but everything I've tried has failed in terms of getting PHP to load the MySQL extension. PHP does work as a module, viewing phpinfo () output the extension dir and php.ini path are correct, but uncommenting the appropriate line (which works in CGI) does not work. Any ideas?
The script isn't accessible to anyone else and before I find a solution to this frigging delay problem I can't be bothered to write anything extra to prevent PHP injections (isn't that what they are called?).
Ah yes, I see how the preg_split and explode would do it, thanks I'm almost certain I didn't have to copy anything anywhere previously when I used PHP 5 and Apache 2 but maybe I'm just dreaming of the way I want things done.. I'll copying the files (I do have libmysql.dll in the ext and php root dirs btw, I'll copy that too).
One more thing though, about newlines. You put "\n" there, but some (file?)systems use other endline chars. Is there any reliable way to determine which ones they are, or is there an abstracted method to read from all these different things?
You could 'detect' the newline type through php_uname(). Splitting by \n and trim()'ing the string is a neat way of getting an identical result on Linux/Unix and Windows. Do Mac's still use only \r ?
Yes, Mac's I think still use \r only (sod 'em, I say ). Currently I read PHPOS to determine the system and hence the endline, but that's kind of faulty since it shows the system PHP was built on. I'll have a look into the php_uname() thingy.
Copying php_mysqli.dll and libmysql.dll to apache/bin did get PHP as a module talking to MySQL. Thank you very much! Also as I suspected, none of that weird delay when accessing files/URLs, it was simply the CGI devil's work.