Archive for 'Php'

Unobtrusive PHP ?

Strange? 

What do you say, if we use this term for a form validation, server side? I say “Unobrusive PHP” because is what I know, but it can be any other language.

Let me explain:

We already do this in javascript. We attach a class to a field we want to validate, then, when the user try to submit, we try to validate the input by a rule associated to the class. All is fine, only that we need to make this server-side too, we cannot trust the users, don’t we.

So what if to the input name, we attach a specific segment, that will be validated once it reach the server, removes the attachment, and pass further the data like never happened?

something like:

<input type=”text” name=”email” />

for js validation would be 

<input type=”text” name=”email” class=”email”/>

for php would be

<input type=”text” name=”email___email” />

and the validation class would take the $_POST array, search for a value  that contains ___email, validates it, then simple remove the segment for further processing.

$_POST["emai"] = $_POST["email___email"];

This idea is the preamble of a next post regarding a more  universal validator for forms.
Is only a concept. Let me now your thoughts.

Textmate - php syntax check

A nice “hidden” feature that Textmate has, is to check the syntax of the php files you are writing, and display a popup with the result.

How to do this?

Open Bundle Editor, search for the PHP bundle, scroll down until you find the Validate syntax command.

Edit the Save setting to Current File.

Now, depending on how you want, you can alter the Activation setting or not. I set it  the same as saving the file, so each time I save the file, it makes me the syntax check.

capture-11

Close the Bundle Editor.

From now on, each time you save, a syntax check will be done, and a popup will show you the result

Like below:

capture-21

That’s all.

How to get the last news from a blog (any blog)

This tutorial will show us how to get the last news of a blog and put it on our site. We may have the case when our site is not fully driven by a blog engine, but we have one, so we want to put on the ‘News’ section the last posts, or we want to grab other’s news feeds and display it on our site.

First step is to download the script built by rborn development, from here. It is a script that is very easy to use, based on LastRSS but changed a little to overcome the restrictive security settings found on many servers related to fopen.
Just include the news_reader.php in the place where you want the news to be. Then edit the file, setting the number of posts and url to grab.

//——– settings ——–

$rssurl = ‘http://feeds.feedburner.com/Cssgalleryinfo’;
$nr = 5;
//————————-

After this you may want to skin this section, because the script is just raw text, this way everybody to be able to skin according to it’s design.

Here is an example, with raw result and a skinned one.

Kroppr - image cropping widget for your site

Kroppr is an unobtrusive script that enhance any site that wants to offer an image cropping tool for his visitors.

Unlike other scripts, Kroppr is able to ROTATE, zoom and move the image in the real time without using plugins - flash or java. The actual picture crop is done after the user is satisfied with his settings. In this way, the load put on the server is minimised and also the visitor’s experience.

The installation  is also very simple , being enough to add a script tag in the header’s page, and a class to the image that makes the cropping subject. More info on the Kroppr page.

How to load ioncube loader without root access

Some of coders are doing a living from selling php scripts. Nothing new here. But to protect themselves from piracy they need to encode the php code, to prevent steal, unauthorized usage and so on.

This can be accomplished using various encoders, some strong , some weak that will give them a specific amount of security, depending on what each of them is doing, form simple obfuscation to byte encoding techniques.

Two major players are Zend Guard and Ioncube encoder.

If zend has a wider spread across the hosting companies, ioncube is not that lucky, and usually you have to make a ticket to support to load the ioncube loader for you ( with or without a fee). There are cases when this cannot be done, so you might be forced to try to load this by yourself.

This can be accomplished if the ini flag enable_dl is ON - a quick phpinfo() should reveal this to you.

If you are lucky, next step is to download the loaders from ioncube site, upload it to your server and point the browser to

http://yousite.com/path/ioncube-loader-helper.php?page=install-assistant

you will find something like this:

ioncube loader

create a php.ini in the folder you need to put encoded files

and add this line to it

if you have the loader in the same folder, path woud be “./” else use the folder where you put the loader.

now point your browser to

http://yousite.com/path/ioncube-loader-helper.php

and if all is ok you should have something like this:

ioncube loader

Pretty simple, no ticket to support and no waiting to be solved.

php “included” menu styling

Some times , when we have a pretty big amount of pages on a site, but don’t need or want to use a CMS, we take advantage of PHP and split the page layout in more includes like:header.phpcontent.phpfooter.phpso we do not need to write the same content ( or copy paste :D ) in every page.If our design is oriented to user( i mean it try to have some usability ) we’ll need to find a way to make the menu clear - pages that are active needs to be “highlighted” somehow.a good example is in wordpress :menu highlighted“Posts” page is current page.We can do this the hard way - copy menu content in every page, and change the styling for current link,or use some php code that do this for us, and move the menu in “header.php” include file.This is a small code we can use for our needs:


">Home ">Product ">Directory

The above code will work for relative links, for absolute links you will have to detect the full url of current page:

<?php $domain = $_SERVER['HTTP_HOST']; $url = "http://" . $domain . $_SERVER['REQUEST_URI'];?>
<div class="actual_menu">    <a href="http://www.cssgallery.info/index.php"
	class="<?php echo (( $url=="http://www.cssgallery.info/index.php") ? "menu_on" : "menu")  ?>">Home</a>
</div>