Archive for the ‘PHP’ Category

Yumeko AdManager 0.0.1

Friday, August 8th, 2008 by Agro Rachmatullah

After a day’s worth of PHP hacking coupled with some swearing, I’ve finally finished a rudimentary ad manager! What does it do? Well, basically it rotates through a pool of ads. A life demo can be seen here. Refresh the demo page several times and see the ads magically rotating.

It’s called Yumeko AdManager because… Well, probably because it will be used on my Japanese learning site Yumeko. Because Google AdSense still don’t allow us to use it on Indonesian web sites.

This program is licensed under GPL 2 and you can download it here (there’s a documentation, yay!). I even made a special page for it here in case I’m inclined to update it.

Here are some of its features:

  • Relative appearance rate of ads can be set
  • Tracks appearance count
  • Tracks click count
  • Expiry date for ads can (uhm… must) be set

What’s obviously lacking is an interface to add and manage ads. Yes, you need to play around using tools such as PHPMyAdmin to add the database rows. But anyway it’s all explained in the documentation :).

Other than PHP, I’m also interested in learning more about this black horse JavaScript to make… ah, let’s call it “interactive programs”. But alas, there’s only so much hours in a day…

HTTP redirection using PHP fails when you use UTF-8 encoding

Tuesday, August 5th, 2008 by Agro Rachmatullah

To redirect a page by putting the instruction on the HTTP header, PHP makes it very easy:

<?php
header("Location: http://www.agronesia.net");
?>

The catch to call header successfully is that you must not output anything beforehand. So, no space before the opening PHP tag. Also no calls to print or echo beforehand. Else you will get an error message like this:

Warning: Cannot modify header information - headers already sent by (output started at H:\xampp\htdocs\bla\test2.php:1) in H:\xampp\htdocs\bla\test2.php on line 2

That also means you cannot have a UTF-8 encoded document. That’s because UTF-8 text files output this BOM thing at the start of the file. Oh, and if you use include, the included files also cannot have any funky BOM stuffs.

You guessed it right. I got tripped by this problem. Because I often type Japanese, I set Notepad++ to use UTF-8 as its default encoding. @#^#&%$@*!!!

PHP variable naming sucks

Tuesday, August 5th, 2008 by Agro Rachmatullah

PHP is a language that deserves curse. For variable names, we always have to use the $ sign. The purpose is of course to add one more thing a programmer can forget. And when you do forget, PHP treats the misnamed variable as a string which in my case causes the script to run but wrongly.

Here’s the golden bug on my script:

$mysql_db = "some_db_name";
mysql_select_db(mysql_db);

No wonder my carefully crafted INSERT query didn’t have any effect…

Global variable scope in PHP

Wednesday, June 11th, 2008 by Agro Rachmatullah

I need to do some WordPress hacking so I’m actually playing around with PHP at this time. What surprised me recently was that this code doesn’t work as I expected:

$foo = 1;
function bar()
{
   var_dump(isset($foo));
}
bar(); // gives bool(false)

It turns out that in PHP global variables are not automatically available inside functions, to prevent accidental overriding. (I have a C-ish background) To use a global variable inside a function, we need to use the global keyword:

(more…)