$developer='Hello Wolrd';

4/4/2013 - SimpleTest vs PHPUnit

Posted in Unspecified

When it comes to Unit testing for PHP you have 2 choices. SimpleTest or PHPUnit, (yes I know there others, ...apparently). Both of those are really good at what they do, and I am not going to compare the various features, that has been done already.
What I will do is tell you my story and why I ultimately chose to use PHPUnit.

When I first started upgrading this site I had no unit tests at all, so I chose SimpleTest because it is really simple to use. You need to include a header file and your tests are good to go.
And this was the way it worked for a few months, I had +600 tests and I was very proud of myself.

But as time went by, I had to do more and more 'difficult' tests, like those using mocking, (admit it, when you start from scratch you always leave mocking to the end).

The other issue I have with SimpleTest is the fact that it has not been updated for well over a year, (at the time of writing this of course). PHPUnit on the other hand is updated all the time.
While I am the first to say that updates all the time is not necessarily a good sign, but no updates at all is also not very good, (and might be an issue as newer version of PHP are eventually released).

Also, phpunit has code coverage, this is a big plus for me :), it allows me to spot potential issues, (where I have no tests at all!), and also where I missed something.

So in short, I would say, go for PHPUnit, it is a little bit more of a learning curve at first, but in the long run you will have better tests that are easier to maintain.

Share |
Technorati Tags: , ,
Filed under: , ,
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

3/27/2013 - Eclipse slow/broken DLTK index

Posted in Unspecified

I use Elcipse 4.2.x for developing my php websites.
But from time to time it re-indexes the workspace, sometimes it never end 're-indexing'.

An easy solution is to look for a folder in your workspace called "../workspace/.metadata/.plugins/org.eclipse.dltk.core.index.sql.h2", (if you don't know where it is, just search for the folder "org.eclipse.dltk.core.index.sql.h2").

Make sure you close Eclipse, rename the folder "org.eclipse.dltk.core.index.sql.h2.old" and restart Eclipse.

The index will be re-created, but once it is done it should be faster.

Share |
Technorati Tags: , ,
Filed under: , ,
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

1/20/2013 - Getting Premature end of script headers in Apache?

Posted in server setup

The other day I was working on a php script, and, for debugging purposes I placed it in a new folder.
Everything was working fine on the dev machines, but on the live server, it was not working at all.

I kept getting a "Error 500 - Internal server error", I even though there might be something wrong with my script, (you start to believe anything, no mater how improbable it might be :)), so I created a new simple script to rule out any code issues.

  1. // simple code
  2. exit( 'hello');

But even that was not working at all... (so at least I knew it wasn't my code).

I knew that nothing had changed on the server, so it had to be something I just did. I created a new folder and I copied a new script into it.
In the end, the answer turned out to be something fairly straight forward, the directory I had created was created with 0777 permissions, (full permissions to do everything!).
My server itself is setup by default not to entertain scripts with such permissions, hence the 500 error.

Change the permission to 0755 and create a very simple script, (like the one above), if that does not help then your issue might be somewhere else...

Don't get me wrong, that might not always be the issue, (there are many other reasons for Internal server errors), but it is worth it to check the folder/file permissions before you start open heart surgery on your server.

Share |
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

1/3/2013 - Windows 'Zombie files' that will give you weird errors.

Posted in Unspecified

The other day I learned something new about the legacy windows file system. There are still some files/folder that you simply cannot create, files like "COM1, COM2, ..., or LPT1, LPT2, CON, AUX, PRN, and NUL", there might be more.
If you try and create/rename the files to one of those name, you will get a cryptic error like "the specified device name is invalid", "The file name already exists" on other programs like SVN you might even mess-up your repo if you try to force one of those name, (SVN will allow the name and windows will give you the finger if you try to update/clean up the file).

The beauty of those files is that it does not matter what the extension might be, in my case I was trying to create a php file, called "cron.php", but that file name is not allowed, (because of the 'cron').

I found a site that has a pseudo-regex you can use to check for valid files "/com\d(\..*)?, /lpt\d(\..*)?, /con(\..*)?, /aux(\..*)?, /prn(\..*)?, or /nul(\..*)?".

In the end it might not be a big issue, but it is something to bear in mind...

Share |
Technorati Tags: ,
Filed under: ,
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

12/3/2012 - Remove duplicate rows in MySQL using php

Posted in mysql

In some cases you might want to remove duplicate rows in a table, (to create a long forgotten UNIQUE index for example).
There are some tricks you can do using a script.

  1. ALTER IGNORE TABLE yourtable ADD UNIQUE INDEX idx_name (some_col );

This does not always work because you might have an older version of MySQL and also, it could lock your table for ages...

An easier way is to use php to remove duplicates but first you need to do a few things

  1. Backup your database.
  2. Remove all codes that create the duplicate rows in the first place.

Create a script, "duplicates.php"

Create the query and fetch the rows

  1. // connect
  2. $mysqli = new mysqli( ... );
  3.  
  4. // prepare the query
  5. $query = <<<EOT
  6.   SELECT count( * ) cnt, link
  7.   FROM yourtable
  8.   GROUP BY some_col
  9.   HAVING cnt >1
  10.   ORDER BY cnt DESC
  11.   LIMIT 100
  12. EOT;

Then loop around and remove all the duplicates, keep going around until you have nothing else to remove.
We only to 100 items at a time, this is to make sure we don't waste all our execution time getting data.

If the code times out, you can just start it again and again until you see the 'done' message.

  1. for(;;)
  2. {
  3. // (re)prepare the query
  4. if( $result = $mysqli->query($query) )
  5. {
  6. $rows = array();
  7.  
  8. // fetch the rows
  9. while ($row = $result->fetch_assoc())
  10. {
  11. $rows[] = $row;
  12. }
  13.  
  14. // free the result.
  15. $result->free();
  16. }
  17.  
  18. // delete all the values
  19. foreach( $rows as $row )
  20. {
  21. // delete one row less than the total number of rows.
  22. $query2 = "DELETE FROM yourtable WHERE somecol='{$row['somecol']}' LIMIT " .((int)$row['cnt'] - 1));
  23. $mysqli->query($query2 );
  24. }
  25. }
  26.  
  27. // clean up
  28. ...
  29.  
  30. echo 'Done!';


If the code times out then simply re-run it, you will not delete more that duplicates and you will not lock the table, (well not too much).

Share |
Technorati Tags: , , ,
Filed under: , , ,
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

11/28/2012 - Does session_start() slow my script?

Posted in php

The short answer is , yes, no, and maybe :).

By default sessions are saved in a file in your temp directory, (check out the value session.save_handler and session.save_path in your php.ini file).
This works great for most small to medium site, when a user visits your site a session file is created and/or the data is updated.

The problem comes when the site gets busier and busier, more and more users are trying to create sessions files. The OS is trying to read/write/delete hundreds of files. And in any case, the OS only allows access to one file at a time, so they run sequentially, not in parallel (*).

So, what is the solution?

  1. Don't use sessions unless you have to, calling session_start() every time will cause issues, (check out session.auto_start).

  2. On really big sites, use database, (or something similar), to manage your sessions.

  3. Even if you think you need sessions, think again...

  4. With social sites like OpenID, LinkedIn, Facebook and so on, sessions are used a lot, so don't think you can create a site with no sessions ...

(*) Most of the time you want it to run sequentially, because if both scripts update the $_SESSION data it is not possible to get/set the data to be shared by both, (Well, not without using a database or something similar).

You can handle sessions yourself using session_set_save_handler.

Share |
Technorati Tags: , ,
Filed under: , ,
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

11/22/2012 - Add cache control to dynamic content in php

Posted in php

Cache control enables you to tell the browser if the contents have changed since the last time they visited.
If nothing changed, then the browser will use whatever data it has in cache and that will save your users a few milliseconds, (and they will a cheer loudly).

For a website like this one, it is important because we have many pages that will no longer change, (archive pages for example, old pages from users that will never return).

First we need to set the cache limiter, the default is to have 'nocache'so you might want to change the value to something like 'public' or 'private'. Read up about the various options before you set it.

  1. // make the data fairly private
  2.  
  3. // start the session.

Now you can go ahead and create your page as you would normally. Don't echo anything out to the browser, rather save it all to a single string.

  1. // create the page
  2. $output = get_my_dynamic_page();
  3.  
  4. // get the time the page was last modified.
  5. // as this is a dynamic page we need to know when last the content changed.
  6. $modified = get_last_modified();
  7.  
  8. // then we need to create a tag for this, (we will use it later).
  9. // create the etag using the output
  10. $eTag = "'".md5($output)."'";
  11.  
  12. // send it to the browser.
  13. header("Etag: ".$eTag);
  14.  
  15. // add the date
  16. $date = gmdate('D, d M Y H:i:s', $last_modified_time);
  17.  
  18. // send the tag+header
  19. header('Last-Modified: '.$date.' GMT');
  20.  
  21. // first we check the check if the etag matches.
  22. if( isset($_SERVER['HTTP_IF_NONE_MATCH']) && $eTag == $_SERVER['HTTP_IF_NONE_MATCH'])
  23. {
  24. // then we check that the modified time matches our own time.
  25. if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) && @strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time)
  26. {
  27. // tell the browser that nothing changed.
  28. header("HTTP/1.0 304 Not Modified");
  29. header('Content-Length: 0');
  30.  
  31. // no need to go any further.
  32. return;
  33. }// modified time match or no modified time was given.
  34. }// etag match
  35.  
  36. // Tell the browser which size the content
  37. header('Content-Length: ' . strlen($output));
  38.  
  39. // if we made it here, echo to the browser.
  40. echo $output;


The code above should be self explanatory,
First, you create your page based on whatever data you might have, be sure to have a date of some sort so you know when it was changed.
Next you create a unique kind id or code to identify if the page that the browser has on record matches the one you have just created.
Finally, if everything matches, then you are golden and you can tell the browser that everything matches.

Be warned, caching is not the magic bullet you think it is, it will not save you a major amount of time, and you need to support cache for your css/js scripts as well as images.

Also, there are many tools going around telling you what the response headers might be, some of them will keep returning 200 because this is the cache response.
I found that Firebug does return the true response header, (what php developer doesn't use Firebug anyway?)

One final word, if you are playing with

 

Share |
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

11/21/2012 - Catch the fatal error: Maximum execution time of 30 seconds exceeded in PHP

Posted in php

I was looking at an issue this morning where the code was timeout in certain situation.
While I could understand the reason for the timeout I was wondering if my code could catch if/when it happens.

Using php5 I can easily test if the error is caught.

  1. // the function that will handle the shutdown
  2. function shutdown()
  3. {
  4. if($a==null)
  5. echo "No errors";
  6. else
  7. print_r($a);
  8. }
  9.  
  10. // Register for shutdown, called when there are no errors and/or there is a timeout issue, (or any fatal issues).
  11.  
  12. // change to 1, default is 30 seconds.
  13. ini_set("max_execution_time",1 );
  14.  
  15. // do nothing for longer than 1 second.
  16. sleep(3);

I then went one step further and logged the error to the database without any problems.

So it seems that the timeout does allow you to catch the execution timeout then do some work.

NB: If you get "No errors" it means that changing the timeout did not work. This is not impossible as certain hosts do not allow certain ini_set values.

Share |
0 CommentsPermanent Link
Share and enjoy
  • Digg
  • del.icio.us
  • DZone
  • Netvouz
  • NewsVine
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • YahooMyWeb

About Me

General development dicsussion

«  March 2017  »
MonTueWedThuFriSatSun
 12345
6789101112
13141516171819
20212223242526
2728293031 

Links

Home
View my profile
Archives
Friends
Email Me
My Wall

Friends

Ant Onaf
DAWNIE
razavi
Alberto
Heather
Silver Wind
Michelle
Bubbles