Friday February 10, 2012 @ 21:00:14 GMT+10    ( Weather:  n/a )
Home » Weblog Archives

Powered byD's Bloggie
Weblog Archive browse by category ...
 → Category :
Display order:
Page 1 of 2   ( 11 entries , showing 1 - 10 )
    1 2 Next  

Fixed several url rewrite problem in Apache - 9:18 pm
also including a "cheat sheet" for it ^^
Bug , Site Issue , Site Updates  -  poster 

Fixed several url rewrite problem in Apache on server side. Apparently that's my mistake, I tried to do something like [0-9\w], which is invalid and will be interpreted as [0-9w]. However, it works perfectly for the new version of Apache on my local computer.

Below is a quick reference or a cheat sheet for Apache mod_rewrite from www.ilovejackdaniels.com :

Apache mod_rewrite cheat sheet, credit goes to  Dave Child.
Enlarge
Apache mod_rewrite cheat sheet, credit goes to Dave Child.

Firefox 1.5.0.6 failed to wrap up a long string - 8:58 pm
Browser , Bug  -  poster 

Stupid Firefox 1.5.0.6 unable to wrap up one long url link in one of the blog entry, causing the whole page has a "wide screen" view.

The fault
http://www.microsoft.com/downloads/details.aspx? familyid=32BC1BEE-A3F9-4C13-9C99-220B62A191EE&displaylang=en

I purposely put a space after the details.aspx? to avoid weird rendering in Firefox browser. Opera and IE wraps/breaks up the string on "-" but obviously Firefox doesn't treat that "-" as a point to break up long string glare

PHP class constructor - 12:33 am
Bug , Coding , PHP , Security , Site Issue  -  poster 

I was doing some code clean up for my site and I found something interesting about the constructor in PHP class.

[ Hide ]
[ Highlight ] [ Text ]
  1. <?php
  2.  
  3. class Site
  4. {
  5. var $_DB;
  6. var $_User;
  7.  
  8. function Site()
  9. {
  10. $this->_DB = new DB();
  11. $this->_User = new User_Authentication();
  12. }
  13.  
  14. function process()
  15. {
  16. // some database access is needed in this function
  17. $this->_DB->query();
  18. }
  19. }
  20.  
  21. class DB
  22. {
  23. // ... some class implementation goes here ...
  24. }
  25.  
  26. class User_Authentication
  27. {
  28. var $_local_DB;
  29.  
  30. function User_Authentication()
  31. {
  32. // We reference to DB object in $SITE if exists,
  33. // otherwise we'll instantiate a new copy of DB locally
  34. if (isset($GLOBALS['mysite']->_DB))
  35. $this->_local_DB =& $GLOBALS['mysite']->_DB;
  36. else
  37. $this->_local_DB = new DB();
  38.  
  39. // A test if the global var $mysite exists or not at this point
  40. echo isset($GLOBALS['mysite']) ? 'Var $mysite is there' : 'The global var $mysite does not exist !';
  41. }
  42.  
  43. function process()
  44. {
  45. // some database access is needed in this function
  46. $this->_local_db->query();
  47. }
  48. }
  49.  
  50. // Let's run the test ...
  51. $mysite = new Site(); // this will be a global variable
  52. ?>

Finish digesting the code ? Let's focus on Line 10 & 11:
10.    $this->_DB = new DB();
11.    $this->_User = new User_Authentication();

... and line 34 - 37
34.    if (isset($GLOBALS['mysite']->_DB))
35.      $this->_local_DB =& $GLOBALS['mysite']->_DB;
36.    else
37.      $this->_local_DB = new DB();

So you would thought that the global var $mysite->_DB is created before the new User_Authentication class is called. As a result, when it comes to line 34, the statement will valid and thus statement on line 35 will be carried out.

But the truth is opposite. You run the script and the output will be The global var $mysite does not exist !.

Why? The key to the problem is on line 11
$this->_User = new User_Authentication();

Although $this->_DB is created after line 10, but that does not mean $mysite->_DB, or even $mysite is listed in PHP defined variable list at that time.

That's because everything happens inside the scope of Site class's constructor. Object are considered 'defined' or 'instantiated' once the script execution exits the class constructor.

That's a hidden surprise if you're not careful enough =O

PHP isset() - 7:40 pm
Bug , Coding , PHP  -  poster 

In PHP, sometimes you'll need to do alot of variable assignment like
[ Hide ]
[ Highlight ] [ Text ]
<?php
// simple
$bar = isset($foo) ? $foo : '';
// complicated
$bar = isset($foo[$test['index']]) ? $foo[$test['index']] : '';
?>


So I came up with this function to ease my life...
[ Hide ]
[ Highlight ] [ Text ]
<?php
// Use this to standardize things..
function isset_empty(&$input)
{
	return isset($input) ? $input : '';
}
?>


Things works fine at first. But later then I discovered that it's not working like what I expected when I pass a value inside an array to that function. See below:
[ Hide ]
[ Highlight ] [ Text ]
<?php
$bar = isset_empty($foo);  // $foo is undefined at this point
echo $bar;
$foo = array(1 => 'World', 2 => 'Cup');
print_r($foo);
$bar = isset_empty($foo[10]);
print_r($foo);
 
// The outpust result
//        <-- emptry string, expected
// Array ( [1] => World [2] => Cup )
// Array ( [1] => World [2] => Cup [10] => )
 
?>


It seems that index 10 with empty string is inserted to the $foo array after calling that function. It doesn't harm in some situation but it's a real disaster if you're using array to populate items or doing a count($foo) to check the array size. The results are affected !

So I have to replace all the statements that uses the function and go back to the old way

$bar = isset($foo[$test['index']]) ?$foo[$test['index']] : '';

Doh! glare

Incorrect weather temperature - 1:02 am
Bug , Site Issue , Site Updates  -  poster 

Fixed the incorrect weather info on my page. It shows -18 degree Celcius whenever the weather data is not available (-18 because of the formula used to convert Fahrenheit to Celcius). It now shows N/A for info if there are no weather data from Weather.com.

Check, check and more checking before you start to process an input.

Redirecting with PHP header() - 11:41 pm
Browser , Bug , PHP , Site Issue , Standards  -  poster 

On the redirection of shoutbox, I've made 2 mistake causing it unable to work on some browsers, example IE 6. In PHP, to do a redirection is very simple.

[ Hide ]
[ Highlight ] [ Text ]
header('Location: http://domain.com/index.php');

However, don't do this [ Hide ]
[ Highlight ] [ Text ]
header('Location: http://domain.com/index.php');
die();


It seems logical if you put a die() after header('Location'). But the truth is, NO you shouldn't ! It will screw your code !
Took me some time to notice the problem. Not sure about header('Refresh') though, I'm calling die() after header('Refresh') and it seems running without problem.

Also remember to use absolute URI for the header instead of relative URI. Browser like Opera accepts relative URI but IE and Firefox ain't too friendly to it. That's the 2nd mistake I've made.

Last edited: Sun 2006-04-02 @ 09:06 , by DaRen 7 time(s)

Redirection on my site - 12:16 am
Browser , Bug , Site Issue , Web Hosting  -  poster 

Oh my god... the Shout box's redirection isn't working in IE 6. I just notice it after my dad told me about this... Before this, nobody encounter any problem ??? Or nobody is surfing my site using IE (impossible) ??? Please report any problem, error or weird CSS rendering and let me know which browser you're using. I can't test my site with all the browsers... takes too much time !

*FIXED*
I made 2 mistakes on the shoutbox redirection script. However the redirection problem in comment posting part is cause by the advertisement codes inserted by the hosting server. They're using window.onload to call their javascript and I'm using the <body onload> to do my redirction, that's why redirection isn't working, they just use my window.onload WITHOUT telling me...hehe... So I remove the onlaod inside the <body> tag and place under their code and it works.

Cause of problem [ Hide ]
[ Highlight ] [ Text ]
<body onload="my_redirect()">
<!-- start of server advertisemt -->
<script type="text/javascript">
    window.onload=their_function()
</script>
<!-- end of server advertisemt -->

Solution [ Hide ]
[ Highlight ] [ Text ]
<body>
<!-- start of server advertisemt -->
<script type="text/javascript">
    window.onload=their_function()
</script>
<!-- end of server advertisemt -->
<script type="text/javascript">
    window.onload=my_redirection()
</script>


Last thing I want to say is, Opera is working fine even there's 2 onload calling, I bet it will work with 3 or more onload inside a script. IE and firefox just can't cope with the extra onload and failed. Opera rulez !

Time zone! Oh come on... - 2:43 am
Bug , PHP , Site Issue , SQL , Standards , Web Hosting  -  poster 

I'm tired of dealing with time zone thing on this server... all my blog entries and calendar don't work out like what I expected... so there's the conversation between me and 100Webspace support team:
Quote:
31 October 2005
Me: Hi. I've noticed that PHP is using GMT+2 time zone but MySQL is set to use GMT+3 time zone. On top of that, apache log is using GMT+3 time zone. Can the time zones be standardized ?
Support: Finding a solution of your problem requires a consultation with our administrators. I have forwarded the issue to them and we will send you our answer as soon as we receive their instructions. If you have any additional questions, do not hesitate to ask. Meanwhile just wait for our next response.
Me: Hi. Thanks for replying. I'll wait for the next reply.
Support: We will contact you when we have solution about this issue.
01 November 2005
Me: Hi. Just wondering hows the progress.
Support: Our administrators haven't replied yet, I will remind them about this issue.
07 November 2005 (a week later...)
Support: The server time is now correct, sorry for the delay.
11 November 2005
Me: (revived) *geez* ... *checking back the access log*, found that the admin already response to it and standardize all the timezone on 31 October as soon as I made the post, but some how the reply from support team reaches me a week later...but at least they still reply, ain't that a good thing ? ;)

Shouldn't they take care of this in early stage when server is set up ?

Last edited: Sat 2005-12-17 @ 03:20 , by DaRen 3 time(s)

Found a bug ? Report to me ! - 7:14 pm
Bug , Site Issue  -  poster 

If you spot any bugs, security exploit, warning/error messages or weird CSS rendering when visiting my site, please kindly report them to me. Please also specify the browser and version of the browser you're using. In other words, I would like to hear from you if your web browser is throwing you with errors (either Windows / Linux / Mac based).

You can leave me a message by posting a comment on this entry or contact me. Thanks happy

ps : not those bugs that crawl on your LCD screen or keyboard ... hehe ...

Last edited: Mon 2005-10-17 @ 19:01 , by DaRen 1 time(s)

PHP Passing by Reference - 3:07 pm
Bug , Coding , PHP  -  poster 

PHP's function supports passing by reference. However, default values may ONLY be passed by reference starting from PHP >= 5. Default values in PHP functions are like:
[ Hide ]
[ Highlight ] [ Text ]
// function declaration with default value
function foo($bar = 'default value here'){ ... };
// function declaration with passing by reference
function foo(&$bar){ ... };


What pissed me off is that I'm using PHP 5 for my computer but the hosting server is using PHP4. I only realize that PHP 4 doesn't supports that after I uploaded all my files to the server. Example of default values passed by reference:
[ Hide ]
[ Highlight ] [ Text ]
// This won't work in PHP < 5
function foo(&$bar = 'default value here'){ ... };


Workaround :
Set allow_call_time_pass_reference to true in php.ini if you're using php >= 5 so that you won't get a bunch of warning messages filling your screen.

Trick to do default values passed by reference [ Hide ]
[ Highlight ] [ Text ]
<?
// function declaration
function foo($bar = 'default value here') { ... };
// function calling
foo(); // valid call
foo(&$var); // this, however is consider deprecated in PHP >= 5
?>


Base on the above code:

PHP < 5 complains:

Parse error: parse error, expecting ')' and later on by a fatal error and killing the script execution.

PHP >= 5 complains:

Warning: Call-time pass-by-reference has been deprecated - argument passed by value; If you would like to pass it by reference, modify the declaration of [runtime function name](). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file. However, future versions may not support this any longer.
Page 1 of 2   ( 11 entries , showing 1 - 10 )
    1 2 Next  
$ view_blog.php 2009.09.17 18:16:41 $
Lost? | XML/HTML sitemap | Contact
38.107.179.241 , 21 queries , 0.1912s
Gzip enabled , CSS compressed , JS compressed
Copyright © 2005-2011 Darren's Outpost