Archive for the ‘Code’ Category

PHP Query String Help

Query strings play important role in building web applications, especially if you want to make nice urls without question mark and many key, value pairs, actually it is not so easy to design application with nice urls however it is always worth doing so, because it not only looks very proffessional, such URLs are search engine friendly, which means that they will get indexed faster but also it happens that search engines have problems with indexing pages with more then 3 key=value pairs in query string.

However, one place where using nice URLs is not neccessary are all kinds of admin panels, there are usually only few admins and more over admin panel never gets indexed so it doesn’t make any sense to make those URLs search engine friendly.

Query string can be accessed thru global array $_SERVER, more specific $_SERVER[‘QUERY_STRING’] is the actual variable where query string is written, this variable contains all data that is inputed after question mark in the URL. For example if we have URL which looks like this:
http://someurl.com/page.php?a=1&b=2&c=3

Then echo $_SERVER[‘QUERY_STRING’] will display: a=1&b=2&c=3, such data is no use to us, we need to parse it, or get it thru global array $_GET, in our case we could write:

echo $_GET['a'];
echo $_GET['b'];
echo $_GET['c'];

which would output:

1
2
3

Two things i have to mention here:
– i do not recommend displaying variables passed by user withut checking if they contain potentially dangerous code.
– make sure you have register_globals set to off in your ini file. If you do not have access to ini file, you can change this setting in your .htaccess file (if you have htaccess files working on your server). To do this just add following line php_flag register_globals off.

Using $_GET array to access variables from query string is pretty simple, however we would want to transform our URL to make it look like this:
http://someurl.com/1/2/3
This is very search engine friendly approach, however like always there are no roses without thorns. First problem is that $_SERVER[‘QUERY_STRING’] variable is empty now, so $_GET array is empty as well.

But first thing first, we can’t just input this URL in web browser address bar, hit enter and wait for a page to load. Probably it would end with 404 Error. To avoid this we need to create .htaccess file in the same directory as page.php file. Then open .htaccess file and write in it:

RewriteEngine on
RewriteRule .* page.php

Now all requests to directory which contains file page.php will be redirected to page.php file. Pretty simple and we can now use URL without question mark or any other unwanted signs i provided earlier (e.g. http://someurl.com/1/2/3).

So as i said earlier now our $_GET array is empty, but fortunately we can still check what the URL looks like by writing this code:

echo $_SERVER['REQUEST_URI'];
// output /1/2/3

Well, maybe URL looks pretty search engine friendly however id does not provide a lot of info as it did earlier. Unfortunately solution to this problem is beyond the scope of this article and i will give you only few guidlines: first you need to parse URL wit explode() to get array, then it will be good to assign some keys to each of the variable, but you always need to remember who is who, do not change order of parameters in the URL.

I covered only two types passing arguments to application with query string, there are other solutions as well, but i think that this two are the most common used and are actually the best when it comes to PHP.

Free PHP Form Generator

For those of you that get bitten by the lazy bug from time to time, here is a great tool to use to generate php based forms:

phpFormGenerator

Its a little tricky to get the hang of, but a nice little tool to play with when you have a lot of projects happening at once and need to free up some time by not coding your own form pocessing with validation.  I would recommend placing the entire form directory on your site and call the form using an iFrame.  This seems to be the cleanest way to do things and will save you about 3 hours of configuration if you try to pull everything apart and disect it.  One other thing worth noting; the phpFormGenerator system does not save your form so you can’t go back and edit it.  I would recommend keeping a browser tab open with your form generator (with generated form) in it until you’ve completely finished with the integration of the form into your site and all is tested and working properly.  If not, you’ll find that you’ll have to remake the entire form scratch.

Enjoy it, but be careful not to cause yourself more work by not paying attention…

PHP and Classic ASP Includes

PHP

<?php include("menu.php"); ?>

Classic ASP

<!--#include virtual="menu.asp"-->