web developing
10 Mar
A typical web developer knows exactly how a PHP if statement looks like:
if ( expression ) { // if the expression was true proceed here } else { // there was a false expression }
When it comes to merge PHP and HTML the things are becoming ugly. Indeed most of the template systems as Smarty are improved and developed to overcome this issue, however when working with a native PHP code with no template system or with template system where the PHP code is allowed the things are really bad.
Let me show this in a breve example. Image you have to show different formatted HTML depending on a PHP expression. Something like that
<?php if (expression) { ?> <div class="message">OK. Your registration is successful</div> <?php } else { ?> <div class="error">Something went wrong! Please try again later! </div> <?php } ?>
Now you can see how difficult to maintain this code is when it doesn’t make use of only one code of HTML markup. Imaging you’ve to print differently formatted tables! Indeed the PHP curly brackets are different to follow.
So there is a PHP syntax that tries to help you manage this. You can write more human readable code like this:
<?php if ( expression ) : ?> <div>message goes here</div> <?php endif ?>
Thus you get the ENDIF instead of only one curly bracket. That’s indeed readable enough. In fact you can use this syntax with any conditional or loop statement in PHP:
<?php foreach($array as $key => $val) : ?> <div class="message"><?php echo $val ?></div><br /> <?php endforeach ?>
To return in the previous example the code above should be transformed in that:
<?php if ( expression ) : ?> <div class="message">some message here!</div> <?php else : ?> <div class="error">some error here!</div> <?php endif ?>
9 Mar
Beside that most of the responses of $_SERVER['HTTP_USER_AGENT'] may return, it appears that this is the most reliable way to track down a user agent with PHP. It is weird that most of the clients, i.e. Safari and Chrome will return something with Mozilla in it’s strings, but however it’s enough to track the “chrome” or “safari” sub strings.
All the examples bellow are from Mac OS X:
Firefox 3.6:
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6
Note: there are both Mozilla and Firefox sub strings!
Safari 4:
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; bg-bg) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/531.21.10
Chrome:
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/532.9 (KHTML, like Gecko) Chrome/5.0.307.11 Safari/532.9
Note: Here they are Mozilla, Chorme and Safari!!!
Opera:
Opera/9.80 (Macintosh; Intel Mac OS X; U; en) Presto/2.2.15 Version/10.10
Nowadays it’s normal to make a site with the presumption it will be visible from mobile. The war between Nexus One from Google and iPhone from Apple is just beginning and with all those devices with wide screens everything’s becoming more complicated.
Both are weird, but both contain the keyword – “mobile” and that may help you make a check with something like this PHP snippet:
<?php $mobile = !!(FALSE !== strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile')); ?>
20 Dec
Recently I posted about the redirecting mechanism of Zend Framework. It’s a working chunk of code but the problem is that it seems quite difficult in according with other ZF sample codes.
Yes the same goal can be achieved with less effort. Like the _forward function which just executes other action/controller, you can call the _redirect method to rewrite the URI.
Just use that in any action of any controller:
$this->_redirect('http://www.example.com');
That executes exactly the same as calling the header PHP method or echoing the <meta> refresher!
27 Nov
Many of the web applications need to be redirected to some given URI sooner or later. In that scenario like redirecting to the profile page after login, which is very common to almost every app, you need to change the URI of the browser after the login form is validated. You can make this by simply using the built in header function in PHP. In other terms the most popular method to redirect in Zend Framework, don’t know why, but this is the _forward method of the controller actions:
$this->_forward('action-name');
The problem here is that thus you get to the given action name, or controller/action but the URI of the browser stays as it is from the caller action.
If you’d like to redirect, like the header function will do it you should use the following construction:
$this->_helper->redirector->gotoRoute(array( 'controller'=> 'contrlname', 'action' =>'actionname'));
27 Oct
As you may know in ZF the urls are strictly organized. First you’ve the module, if you use modules. If there is only one module, and it is normally the default one, you omit the module name from the url. Than comes the controller name, the action name in that particular controller and than separate by / the url continues with key value pairs of the get parameters.
Of course to make this all work, you’d need to setup a simple .htaccess file just to rewrite all this to the index.php file which than parses the $_SERVER['REQUEST_URI'] to collect all the data he need about which controller, which action and what parameters to call.
That’s pretty simple. If you’ve the following URL /user/profile/name/john.smith you may suppose that Zend Framework calls the “user” controller, “profile” action with get parameter “name” which equals to “john.smith”. In a traditional PHP style old school URL this can look like this:
?controller=user&action=profile&name=john.smith
The problem is that Zend reads all this, parses it and than returns the correct output. But however not always this is what we want, right? I’d like to have custom url for the profile page. Why should I need both profile and name in my URL, I’d prefer a URL like this: /user/john.smith. (more…)