Web Development

Web Development Tips & Tricks, the things that you don’t want to figure out.





Archive for April, 2007

PHP – Unsetting a Cookie Array

Sunday, April 29th, 2007

Hi everyone,

Most of what I’ve been doing recently I can’t blog about because it’s not complete, and who would want a half written blog?

What I am blogging today is a little loop that will unset a particular array in the Cookie array. If this sounds confusing, I’m not surprised. All the cookies are part of the Cookie Array. You can make subarrays within the Cookie arrays. For instance:

$nextWeek = time() + (7 * 24 * 60 * 60);
setcookie(“clothes[shirts]“, “blue”, $nextWeek, “/”);
setcookie(“clothes[shirts]“, “black”, $nextWeek, “/”);
setcookie(“clothes[shirts]“, “red”, $nextWeek, “/”);

And then, to make it a little more complicated, suppose we added another part to that array:

setcookie(“clothes[pants]“, “small”, $nextWeek, “/”);
setcookie(“clothes[pants]“, “medium”, $nextWeek, “/”);
setcookie(“clothes[pants]“, “large”, $nextWeek, “/”);

Now, lets say we wanted to just unset the “pants” subarray. Here’s a loop that could do it:

$subarray = “pants”;
if(isset($_COOKIE['clothes']) and is_array($_COOKIE['clothes']))
{
foreach($_COOKIE["items"][$subarray] as $key => $value)
{
$result[$key] = setcookie(“items[" . trim($subarray) . "][" . trim($key) . "]“, “”, time() – 3600, “/”);
}
}

I may add on to this post later and post an unset function.

-Kerry

Photoshop 7 Bug (Too Many Fonts?)

Wednesday, April 25th, 2007

Hello once again!

I have very exciting news about Photoshop! Ok, only exciting if your photoshop hasn’t been working for the past six months and you finally figured out the bug.

Apparently Windows doesn’t like to have more than 1000 fonts loaded in the system. Takes up too much memory. Here’s a chart on what causes system delays: What Slows Windows Down? I was without this datum until yesterday, and I had loaded 5000+ fonts onto my system from a package my friend gave me.

My friend dug up this article on photoshop problems (he was having the same problem as me) and removed most the entire package of fonts and just left the originals, and voila! It worked! I tried it and it worked as well!

My lesson has definitely be learned. I hope this blog helps some people out.

-Kerry

PHP MCrypt (Encryption)

Tuesday, April 24th, 2007

Wow! Haven’t posted in a while. I should start more frequently, I’ve had some very busy days.

As I touched on in a previous post, I was working an encryption. I had found that GnuPG (a free version of PGP – Pretty Good Privacy) could be used in PHP and was trying to get that working. I looked into it and it seemed pretty complicated and I didn’t easily have access to install the necessary modules on on Linux Hosting. Therefore I decided to look for an alternate method.

I needed two-way encryption, meaning I could encrypt and decrypt it. The PHP function crypt() is one-way encryption, and wouldn’t work for my needs. MCrypt(), however, is a two way encryption function that supports many different methods. To see what methods are available on your server, put in the phpinfo() function in a page and you will find a section called “mcrypt”. Here it will tell you what methods are available for your use.

A page from hudzilla.org goes over the main different encryption methods and what should be used for your situation. I ended up choosing rijndael-256 (256 bit encryption, which is quite powerful). The example I give below will use this method.

I have made two functions, one to encrypt a string and one to decrypt a string. Here they are:

function encrypt($string, $extra = “”)
{
/* Open the cipher */
$td = mcrypt_module_open(‘rijndael-256′, ”, ‘ecb’, ”);

/* Create the IV and determine the keysize length, used MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);

/* Create key */
$key = substr(md5(“CoMpLiCatED K3y” . $extra), 0, $ks);

/* Intialize encryption */
mcrypt_generic_init($td, $key, $iv);

/* Encrypt data */
$encrypted = mcrypt_generic($td, trim($string));

/* Terminate encryption handler */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);
return $encrypted;
}

Where “CoMpLiCatED K3y” is your key for encryption and decryption. The optional $extra argument is what you can append to your key. This can make something more secure and different encryption keys for different purposes. Here’s the decrypt function:

function decrypt($encrypted, $extra = “”)
{
/* Open the cipher */
$td = mcrypt_module_open(‘rijndael-256′, ”, ‘ecb’, ”);

/* Create the IV and determine the keysize length, used MCRYPT_RAND
* on Windows instead */
$iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_DEV_RANDOM);
$ks = mcrypt_enc_get_key_size($td);

/* Create key */
$key = substr(md5(“CoMpLiCatED K3y” . $extra), 0, $ks);

/* Initialize encryption module for decryption */
mcrypt_generic_init($td, $key, $iv);

/* Decrypt encrypted string */
$decrypted = mdecrypt_generic($td, $encrypted);

/* Terminate decryption handle and close module */
mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return trim($decrypted);
}

The same data applies to the above function, and it returns the decrypted string.

Enjoy!
-Kerry

PHP Ternary Operator – A Rare Find

Monday, April 16th, 2007

Today I discovered the ternary operator, which I’ve seen once before, while studying C++.

The ternary operator is a one-line way to do an if else statement, and I found it very good for cutting down unnecessary code. For example, if you had the statement:

if($color == “blue”) {
$correct = true;
} else {
$correct = false;
}

You could make it something much simpler:

$color = ($color == “blue”) ? true : false;

The way the ternary operator works is: “condition ? (expression if true) : (expression if false);”. You can assign variables to it like I did, or simple print it out. I haven’t tested this yet, but I’m pretty sure you can put functions there (I don’t see why you couldn’t).

Anyway, not a big post, but I thought it would be a useful operator to know about.

-Kerry

Dynamic Content and Static URLs

Saturday, April 14th, 2007

SEO at its best!

Search engines will look at Dynamic pages as their own pages (with their own query strings), and index them as such. The advantage to having a Static URL is you can use keywords in the title which well help with your SEO. For instance, if you had an article about global warming, you might want the name to be “http://www.yoursite.com/global-warming.php”. But you don’t want to have to actually create a static page for each of those URLs.

There are two ways of doing this, one better than the other. What I had been doing for a long time is make a static page, then including the dynamic page and passing variables to it. Something like:

<?php
$articleid = 27;
include(“dynamic-page.php”);
?>

The dynamic page is coded to take that variable and then it displays the page. This is definitely better then redoing the code every page, but it can be improved.

.htaccess is the key to this solution. It can only be used on Apache servers as far as I know (sorry to those of you who have a Windows Server). Regular Expressions are also used in .htaccess, and will be in this example. The basis of this solution is you use .htaccess to redirect static links to a dynamic page, but when you redirect the links address doesn’t change. Here’s an example of how you can do that (put this code in your .htaccess file).

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)-a-([0-9]*).php$ /dynamic-page.php?articleid=$2 [L]

Ok, basically says any link to .php that ends with “-a-” (number) “.php” will be sent to your dynamic page, with the query string “articleid=” (number). So, lets say you went to “global-warming-a-27.php” it would redirect the page to “/dynamic-page.php?articleid=27″. Now your page can use that ID to call the right article from the database or wherever you’re storing the article.

That’s it! It’s as simple as that. This will allow you to create static urls, so Search Engines can find them and people can book mark them, but you don’t have to create any extra code.

-Kerry

Another Regex

Tuesday, April 10th, 2007

Hello All,

You may have noticed that I’ve been writing less posts recently. That’s because of two reasons: I’ve been doing a lot of other activities, and the project I’m working on is a long one. I usually write about something that I’m working on every couple of days. I’ve been building a shopping cart, so I’ve be having several posts about Paypal integration, setting up various accounts and so on.

Today’s post is a small one, just another regular expression I made. Here it is:

^[-+](?:[0-9]+(?:\.[0-9]{1,2})?|(?:\.[0-9]{1,2}){1})$

This will match amounts that are lead by a + or – symbol. For example, it will match these: “+3″, “+599.99″, “-4.45″, or “-.5″. It will not match “3″, “-.”, “+3.356″ etc.

I hope this will be of some benefit to you.

-Kerry

Reading XML – XSL or PHP?

Friday, April 6th, 2007

XML, or Extensible Markup Language, is probably the most versatile language there is.

Since you make your own tags, and then get readers to use them, you can use the data in them for pretty much anything. Well, today I was making my php reader display a bunch of URLs based on a category they were in. They also had special attributes and so on, all of which was contained in an XML file.

As I was looking at my sitemap, I realized that it had formatting, but it was an XML, and XML files can’t format themselves. So, I found out that it called an XSL (or Extensible Stylesheet Language) file that gave it formatting. XSL is a language used to make XML in HTML or XHTML. Call glorified HTML or XHTML (has in built functions to read XML). You can make entire pages, or parts of pages in XHTML and call in the data from an XML sheet.

If this is the language, why should you use the roundabout method offered in PHP? It took me some time thinking and wondering the advantages of each. Realise, I don’t know XSL (I started learning today), so I don’t know its full functionality. I do know, however, that you can see the XSL file and you can see the XML file. There’s a difference.

PHP code is server side, meaning completely executed before it reaches you, so you can’t see it. This includes the call to the XML file. This means you can display data based on an XML file without letting the surfer know that you called an XML file. This is a security feature, which I kind of enjoy for my current purpose of the URLs.

Now, I assume that XSL has a lot more functionality than PHP, seeing as how it is a language specifically to convert XML into HTML or XHTML. PHP is a complete programming language. It actually didn’t have a lot of it’s support for XML until PHP5 came out.

One more thing while we’re on the subject of URLs. I needed to grab just the domain name out of a url. For example in “http://www.google.com” I want just “google.com”. In the case of “http://mail.google.co.uk” should match “mail.google.co.uk”. I decided to see if I could put my regex skills to work, and I came up with this: “(?:www\.)?([^.\/]+\.(?:[a-zA-Z]+\.)*(?:[a-zA-Z]{2}\.[a-zA-Z]{2}|[a-zA-Z]{2,4}))” which matches both of those examples. You simply have to grab the return value (there’s only one) and it will have the right result.

-Kerry

Courtesy

Monday, April 2nd, 2007

Hello once again,

Today’s blog is about courtesy. I’ve been trying to get the logo for my company started so I can make the site around it, which is one of the first steps in the design.

I found the logo I want on deviantART, which is a great logo. A friend of mine touched it up, and I loved it, so I thought it would be a pretty simple deal asking permission from the artist. I sent him a note, asking him if he would mind if used it as my business logo.

A couple days go by… no response.

So, I went on and checked to see if he was an active user. Yep, he responded to comments within the last day or so. I thought, maybe he doesn’t check messages, but comments. So, I left him a comment.

A week and a half go by… no response yet again.

I check, he’s still active. I decide to leave him another. 6 days later and it’s today. I’m still waiting for a response, and I sent him another note. I don’t see what the problem is, if he doesn’t want me to, he could simply say so. If he wants something for it, he could say “yes, but unfortunately not for free” or something like that. Or, he could just say yes. Simple communications, definitely doesn’t take the 19 days I’ve been waiting.

Aside from that, you’re be getting more on regular expresisons in a day or so, so I hope you enjoy!

-Kerry