Saturday February 11, 2012 @ 10:06:45 GMT+10 ( Weather: n/a )
|
|
|
|
|
Powered byD's Bloggie
| Weblog Archive browse by category ... |
|
|
|
|
PHP class constructor - 12:33 am
Bug , Coding , PHP , Security , Site Issue - DaRen
I was doing some code clean up for my site and I found something interesting about the constructor in PHP class.
[ Hide ] <?php class Site { var $_DB; var $_User; function Site() { $this->_DB = new DB(); $this->_User = new User_Authentication(); } function process() { // some database access is needed in this function $this->_DB->query(); } } class DB { // ... some class implementation goes here ... } class User_Authentication { var $_local_DB; function User_Authentication() { // We reference to DB object in $SITE if exists, // otherwise we'll instantiate a new copy of DB locally if (isset($GLOBALS['mysite']->_DB )) $this->_local_DB =& $GLOBALS['mysite']->_DB; else $this->_local_DB = new DB(); // A test if the global var $mysite exists or not at this point echo isset($GLOBALS['mysite']) ? 'Var $mysite is there' : 'The global var $mysite does not exist !'; } function process() { // some database access is needed in this function $this->_local_db->query(); } } // Let's run the test ... $mysite = new Site(); // this will be a global variable ?>
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
Site updates on long weekend - 7:38 pm
New [table] tag
Site Issue , Site Updates - DaRen
Today is a public holiday, the Queen's birthday. Spent most of yesterday and today's time to update my site. I added a [ table ] tag to my bbcode parser. This table tag is inspired by the table syntax used in Wiki. Wiki called it as 'pipe syntax'. See below :
[ Hide ] [ table]
{| class="weblogtable" style="margin: 1em auto 1em auto;"
|+ The table's caption
! Column heading 1 !! Column heading 2 !! Column heading 3
|-
! Row heading 1
| Cell 2 || style="font-size: 20px;" | Cell 3
|- style="color:#ff0000;"
! Row heading A
| Cell B
| Cell C
|}
[/table]
Will give you this result:
The table's caption
| Column heading 1 |
Column heading 2 |
Column heading 3 |
| Row heading 1 |
Cell 2 |
Cell 3 |
| Row heading A |
Cell B |
Cell C |
This is quite different from the existing tags. It's sort of half tag half markup style. Everything is enclosed inside the table tag, however the syntax is more to structured markup style. Line break is important, every line must start with a valid syntax. The syntax is almost 95% the same as Wiki has. Nested tables are allowed inside a table cell. Check out Wiki - Help:Table. To see the how I used the table tag, visit here and also here.
Added encoding checking in blog content. It will display a icon telling whether the content is pure Ascii, UTF-8 wellformed or just a malformed UTF-8 (might be other type of encoding). I also made a feature that allows public to see the raw structure of a blog entry. It will dump out what it's originally looks like before rendering is took place. Search the top-right of every blog entries for the icons.
Iñtërnâtiônàlizætiøn - 1:28 pm
Everything is put up for testing...
Site Issue , Site Updates , Unicode , Web Hosting - DaRen
Don't be surprised with all those chinese characters appearing all over the main page. I'm just performing a test to see how well my template engine can handle with multibyte characters.
The main content/layout will still be in Engish (well, technically speaking, ASCII). I'll probably use some chinese in some of the blog entries if needed.
Oh ya, Iñtërnâtiônàlizætiøn
The MySQL database dump seems to be working fine. However, in server-side, I haven't tried to restore the data from the dump file yet. I'll try it later or tonight since I have something else to do right now =)
Last edited: Sat 2006-06-10 @ 13:34 , by DaRen 3 time(s)
Unicode Testing - 10:21 am
Encoding used : UTF-8
Browser , Site Issue , Site Updates , Unicode - DaRen
Running Unicode test on server-side... If you see gibberish below that means your browser doesn't support the font... or just simply my code isn't working like what I expected...
English
You kidding me?
Chinese (Simplified)
花间一壶酒,独酌无相亲
举杯邀明月,对影成三人
月既不解饮,影徒随我身
暂伴月将影,行乐须及春
我歌月徘徊,我舞影零乱
醒时同交欢,醉后各分散
永结无情游,相期邈云汉
Chinese (Traditional)
子曰:「學而時習之,不亦說乎?有朋自遠方來,不亦樂乎?
人不知而不慍,不亦君子乎?」
有子曰:「其為人也孝弟,而好犯上者,鮮矣;
不好犯上,而好作亂者,未之有也。君子務本,本立而道生。
孝弟也者,其為仁之本與!」
Russian
По оживлённым берегам
Громады стройные теснятся
Дворцов и башен; корабли
Толпой со всех концов земли
К богатым пристаням стремятся;
Ancient Greek
Ἰοὺ ἰού· τὰ πάντʼ ἂν ἐξήκοι σαφῆ.
Ὦ φῶς, τελευταῖόν σε προσϐλέψαιμι νῦν,
ὅστις πέφασμαι φύς τʼ ἀφʼ ὧν οὐ χρῆν, ξὺν οἷς τʼ
οὐ χρῆν ὁμιλῶν, οὕς τέ μʼ οὐκ ἔδει κτανών.
Sanskrit (should be right-aligned)
पशुपतिरपि तान्यहानि कृच्छ्राद्
अगमयदद्रिसुतासमागमोत्कः ।
कमपरमवशं न विप्रकुर्युर्
विभुमपि तं यदमी स्पृशन्ति भावाः ॥
Tamil
ஸ்றீனிவாஸ ராமானுஜன் ஐயங்கார
Czech
Dejte mi libru kávy Java, dvě libry Rio, šest liber kouskového cukru a paklíček cikorie.
Korean
비겁한 사람은 죽기 전에도 여러번 죽는다.
그러나 용감한 사람은 한번 밖에 죽지 않는다.
Japanese
コンビニエンスストア全国大手のファミリーマートの道内1号店が7月7日に札幌市東区本町1で開店することが9日、明らかになった。これによりファミリーマートは全都道府県出店を達成し、7月14日には同市清田区美しが丘4に2号店も開く。
ps: The font works fine in my Opera 8.50 browser. Firefox 1.0.7 is rendering the text a bit smaller making some of the characters look weird. Internet Explorer 6 is displaying bigger text size, but the Ancient Greek part is partially gibberish. In other words, Opera still rules ! ^^
PHP, MySQL, Unicode - 9:14 pm
Complicated ? nah... it's VERY complicated
(X)HTML , Site Issue , Unicode , Web Hosting - DaRen
Serving my site content in XHTML in Unicode has already been on my to-do list for quite a while. I have read alot of articles on how to implement it. Sadly, 90% of the hosting companies are still using old PHP, old MySQL server and not having Multibyte String Functions enabled in PHP. They are not aware of , or just don't like the idea of "Internationalization" eh ?
After checking the specs of my hosting server, PHP seems to be OK but I am not sure about MySQL. It would be waste of time if the server is running a older version of MySQL that completely treats Unicode as something that came from outter space. Anyhow, I'll spend some time on this weekend to work on it. I just love the idea of having having Chinese characters on my site on top of English. Unicode will be my only option to do that. I don't like to serve the page with content="text/html; charset=gb2312", I just need something that can do all the things - Unicode.
By the way, I changed the top horizontal navigation menu on my site. I haven't upload it to this server though. Everything is still on localhost =) The one that's still on this site, it's too "heavy" and runs too slow. It consumes a noticeable amount of CPU performance. The new one will be alot more lightweight and most important thing is, it's still cross browser compatible.
Site will be updated pretty soon... hopefully...
Site accessibility updates - 11:40 pm
The <link>
Site Issue , Site Updates , Standards - DaRen
Some updates to my site. I added a horizontal navigation menu on the top. Added additional <link> tags in <head> so that some browser can pick it up and display it in navigation bar for accessibility. Examples of them are 'Home', 'Help', 'Author', 'First', 'Previous', 'Next', 'Last'.
Also added an "About" page to my site. Read the about page.
ahkuan.com OWNED! - 9:32 pm
ahkuan.hollosite.com is history
Site Issue , Site Updates - DaRen
The old site is "officially" closed. Everything is moved to this new server. Took me a month to make all these working which I didn't expect. Besides moving the site, I redesign the site logo, minor cosmetic changes and changed lots of codes that makes my script more versatile to adapt to the new environment. That includes altering tables in database.
However, the major update will be the opening of Public Upload section ! At first I'm planning to make it as a private upload section for myself to store some temporary files other things but since I got plenty of space on this server, might as well I open it to the public. So if you need a place to store files, you're welcome to use the service as long as you agree with the disclaimer.
The public upload service is powered by D's File Manager. It's a powerful but simple "module" to use. My web-based file manager and weblog file uploads rely on it. Let me know if you found any security holes for example like, manipulate a file that's outside the directory scope =) I would be very happy to know that.
Here's the public upload section, or you can access it through the left navigation panel on my site. Maximum upload size is unknow but it should be 10Mb per upload.
Linux filesystem and permissions - 7:50 pm
Owner ? Group ? Who owns who ?
Computing , Security , Site Issue , Web Hosting - DaRen
I'm still having trouble with my new hosting environment. They're using different configuration in some areas and taking me time to do Q&A between me and the support team.
For example, in my new hosting environment, PHP is run under the ownership of "Nobody". That means the folders/files created using PHP script has the group 'Nobody' and owner 'Nobody' in Linux filesystem. That doesn't hurt but it's a pain in the ass when it comes to file manipulation through FTP client. That's because when you login through FTP, you belongs to your username group. Thus making you having no rights to manipulate to those files that are create by PHP script. Sounds stupid eh ? (See here)
But that's not the end of the story. If you upload/create the files through FTP, you can't delete them through PHP script because of the owner/group permission issue! That's really stupid but then the answer/solution I got from the support team is
Quote:You have to first connect to the FTP server from the PHP code in the script . Then with the php script you can create the file/folder. So when you are connected to ftp server and then the file/folder is created then the file/folder has the ownership of the ftp user that you have used in the script. So you will be able to delete this file created from the FTP client.
*Fainted... ... ... ... ... ...*
I suggest them to use suexec or suPHP but I doubt they will ever care about that. Any Linux guru can help me ?
New address !! - 5:50 pm
http://ahkuan.com <--- hot from oven
Site Issue , Site Updates , Web Hosting - DaRen
Woooohooooo ~~
I'm moving... same as my weblog... Checkout
Finally I got a domain name that "belongs" to me. Well, I can't really owned it, I have to rent it for 1 year =) I'll try to get it working on the weekends, but apparently I won't have time on the weekend as I need to clean up the mess of my house.
This is the first time I have more control over my site... got alot of things to learn ahead.. Oh baby, this rocks my world !!!
ps: If you ever wonder where is the www after http:// .. I can tell you that www is history
darrensoutpost.com OR ahkuan.com - 7:36 pm
which is better ?
Site Issue , Web Hosting - DaRen
I'm planning to register a domain name. Which one sounds better ?
darrensoutpost.com
or
ahkuan.com
Personally I prefer darrensoutpost.com although it's a bit long. Found a web hosting company in Australia, MD Web Hosting. The price for personal package is acceptable. I'll ring them tomorrow and ask about the server specs and also the software specs.
Let me know what you think =)
|
|
|
|
|