Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Failing to write all data to file

Posted by dabbler (dabbler), 30 May 2003
Hi Graham.

After 3 hours of picking through code and the php manual, I am stuck. Please forgive me if this post is not in following with the intended nature of this forum. It's my last resort. I'd greatly appreciate a little help with this.

I have a set of scripts that run a banner management system. It's great but for one problem that is making unusable. It looks to me like it could be something simple that I just don't know enough to see. Or it's something to do with having an NT server that requires some small change.

Ads.dat holds all of the data for the scripts, the configuration data, and the ad data. When using the admin area, saving the config changes writes to file as it should. The problem is when the ad data (specifically clickthrus) changes and the file is rewritten, it omits the config data. I think I've narrowed it down to two smaller problem areas.

Click.php, which logs the click and redirects
for ($i = 0; $i < count($ads); $i++) {
   if(ereg("^$id\|\|", $ads[$i])) {
       $data = explode('||', $ads[$i]);
       $ads[$i] = join('||', $data);
if (!$data[9]) {
Header("Location: $data[9]");

And then in
$ads = array();
$configAds = array();
$AdsTime = time();
$lines = file($AdsDatFile) or die();
foreach ($lines as $line) {
   $line = chop($line);
   if (($line != '') or (!ereg('^#', $line))) {
       if (ereg('^[0-9]', $line)) {
           $ads[] = $line;
       } else {
           list ($key, $val) = split('=', $line);
           $configAds[$key] = $val;
function writeads()
   global $AdsDatFile, $ads, $configAds;
   $data = fopen($AdsDatFile, 'w') or die();
   flock($data, 2) or die();
   fputs($data, @join("\n", $ads)."\n");
   while (list ($key, $val) = each ($configAds)) {
       if (($key != '') && ($val != '')) {
           fputs($data, $key.'='.$val."\n");

Is there an obvious error here?   Everything is fine until a click, and it logs the click. Just loses the $key and $val data somehow. Any help would be greatly appreciated.


Posted by admin (Graham Ellis), 30 May 2003
Hi ... welcome ... and exactly the right use of the forum   ... interesting problem too.

Educated guess:   add the line

before the while (....) line in the writeads function.

In PHP, the each function returns a key / value pair from an array each time you call it, and it returns a false value when it runs out of data.   So far, so good ... except that when you enter such a loop it starts from wherever it happened to reach last time you used each on the same array last.  The frighteningly named "reset" simply resets the internal pointer that each uses so that it starts (again) at the beginning.   There are time where you have to do a reset before a loop of each-es, even if you've never eached through the array before.   I don't think the pointer is automatically initislised when an array is created.

Anyhow - if that's it, great ... if not,  let me know and I'll have a further look (I would probably end up suggesting creating an extra temporary file to write variable into to see more about where the failure is occurring ....)

Posted by dabbler (dabbler), 30 May 2003
Ahhhh, and fantastic, that did the trick.

Thank you. Now everything keeps displaying properly.

It never fails that when I pick up a free script, I have to spend time customizing it to make it work for me the way I'd like, which has been good, I've learned a lot this way.

Thanks again, you're a star.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho