CSS: having a popup appear on graphic mouse over

You have a graphic that if the user mouseovers you want a popup.

How the heck do you do that?

your original code looks like this:

<img align="center" src="img/dracko120.jpg">

You need to declare a div with a lable for the image file and what you want to do

assign a dumy href command (that goes nowhere)
put what you want to do between the span labels.

so my code now looks like this:

<div id="searchpop">
<a href="#">
<img align="center" src="img/dracko120.jpg" />
<?php $dracko->dsp_search_form(); ?>

Now I have to lay out the css

the first entry in the css is the div id, the a in <a href="#">, the span (this says don’t display)
the second entry tells it what to do on mouse over

#searchpop a span {display: none;}

#searchpop a:hover span {display: block;

position: absolute; top: 200px; left: 0; width: 125px;

padding: 5px; margin: 10px; z-index: 100;

color: #AAA; background: black;

font: 10px Verdana, sans-serif; text-align: center;


—have fun

How to get a file extension using php

Question : How to get file extension using php
I want to extract the file extentions from a file name. How do i do that from php?

There are several ways to do that. First is using the combination of strrpos() and substr() function like this :

$ext = substr($fileName, strrpos($fileName, ‘.’) + 1);

For example, if $fileName is my-new-house.jpg then strrpos($fileName, ‘.’) will return the last location a dot character in $fileName which is 15. So substr($fileName, strrpos($fileName, ‘.’) + 1) equals to substr($fileName, 16) which return ‘jpg’

The second is using strrchr() and substr() :

$ext = substr(strrchr($fileName, ‘.’), 1);

strrchr($fileName) returns ‘.jpg’ so substr(strrchr($fileName, ‘.’), 1) equals to substr(‘.jpg’, 1) which returns ‘jpg’

PHP Forms and User Input

PHP Forms and User Input

The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input.
PHP Form Handling

The most important thing to notice when dealing with HTML forms and PHP is that any form element in an HTML page will automatically be available to your PHP scripts.

Form example:


<form action=\"welcome.php\" method=\"post\">
Name: <input type=\"text\" name=\"name\" />
Age: <input type=\"text\" name=\"age\" />
<input type=\"submit\" />


The example HTML page above contains two input fields and a submit button. When the user fills in this form and click on the submit button, the form data is sent to the \"welcome.php\" file.

The \"welcome.php\" file looks like this:


Welcome <?php echo $_POST[\"name\"]; ?>.<br />
You are <?php echo $_POST[\"age\"]; ?> years old.


A sample output of the above script may be:

Welcome John.
You are 28 years old.

The PHP $_GET and $_POST variables will be explained in the next chapters.
Form Validation

User input should be validated whenever possible. Client side validation is faster, and will reduce server load.

However, any site that gets enough traffic to worry about server resources, may also need to worry about site security. You should always use server side validation if the form accesses a database.

A good way to validate a form on the server is to post the form to itself, instead of jumping to a different page. The user will then get the error messages on the same page as the form. This

Solve PHP Fatal error: Allowed memory size of 8388608 bytes exhausted

This error message can spring up in a previously functional PHP script when the memory requirements exceed the default 8MB limit. Don’t fret, though, because this is an easy problem to overcome.

To change the memory limit for one specific script by including a line such as this at the top of the script:


The 12M sets the limit to 12 megabytes (12582912 bytes). If this doesn’t work, keep increasing the memory limit until your script fits or your server squeals for mercy.

You can also make this change permanently for all PHP scripts running on the server by adding a line like this to the server’s php.ini file:

memory_limit = 12M

Keep in mind that a huge memory limit is a poor substitute for good coding. A poorly written script may inefficiently squander memory which can cause severe problems for frequently executed scripts. However, some applications are run infrequently and require lots of memory like importing and processing a big data file.

PHP: Sending Email with Text/HTML/Attachments

PHP: Sending Email (Text/HTML/Attachments)

Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.

So, you want to send automated email messages from your PHP application. This can be in direct response to a user’s action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated.

* Sending a Simple Text Email
* Sending HTML Email
* Sending Email with Attachments

Note that to send email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in your php.ini file to point to your email server.

Sending a Simple Text Email

At first let’s consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. The mail() function returns True if the message is sent successfully and False otherwise. Have a look at the example:

As you can see, it very easy to send an email. You can add more receivers by either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers. If you don’t receive the test mail, you have probably installed PHP incorrectly, or may not have permission to send emails.

Back to top

Sending HTML Email

The next step is to examine how to send HTML email. However, some mail clients cannot understand HTML emails. Therefore it is best to send any HTML email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If your customers have HTML email turned off, they will still get a nice email, even if they don’t get all of the HTML markup. Have a look at the example:

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!

This is something with HTML formatting.


In the preceding example we add one additional header of Content-type:multipart/alternative and boundary string that marks the different areas of the email. Note that the content type of the message itself is sent as a mail header, while the content types of the individual parts of the message are embedded in the message itself. This way, mail clients can decide which part of the message they want to display.

Sending Email with Attachment

The last variation that we will consider is email with attachments. To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email. Have a look at the example:

Content-Type: multipart/alternative; boundary="PHP-alt-"

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!

This is something with HTML formatting.


Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment


As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64, split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.

Encrypt Passwords in the Database

How to Encrypt Passwords in the Database

If you are developing a password-protected web site, you have to make a decision about how to store user password information securely.

What is "secure," anyway? Realize that the data in your database is not safe. What if the password to the database is compromised? Then your entire user password database will be compromised as well. Even if you are quite certain of the security of your database, your users’ passwords are still accessible to all administrators who work at the Web hosting company where your database is hosted. Scrambling the passwords using some home-brewed algorithm may add some obscurity but not true "security." Another approach would be to encrypt all passwords in your database using some industry-standard cipher, such as the Message-Digest Algorithm 5 (MD5).

MD5 encryption is a one-way hashing algorithm. Two important properties of the MD5 algorithm are that it is impossible to revert back an encrypted output to the initial, plain-text input, and that any given input always maps to the same encrypted value. This ensures that the passwords stored on the server cannot be deciphered by anyone. This way, even if an attacker gains reading permission to the user table, it will do him no good.

MD5 does have its weaknesses. MD5 encryption is not infallible: if the password is not strong enough, a brute force attack can still reveal it. So, you can ask: "Why should I use MD5 if I know it is not the most secure?" The answer is fairly straightforward: it’s fast, it’s easy, and it can be powerful if salted. The greatest advantage of MD5 is its speed and ease of use.

It is vitally important to understand that password encryption will not protect your website, it can protect your passwords only. If your website does not have sufficient protection, password encryption will not make it safe from cracking. If your system has been cracked, a hacker can inflict a irreparable damage to it and also gain an access to confidential information, including passwords database. But if you store this information encrypted, hackers practically cannot make use of it. Cracking an encrypted password takes a large amount of time and processing power, even on today’s computers.

So, let’s start. First of all, you need to add a new account to your database. The following code allows to do it.

Now, when a new user completes the registration form, his password will be encrypted automatically.

After that we should write code that validates a given username/password pair.

And what if you already have users’ database ready and want to start using encrypted passwords? To do it, you need to write encypt.php script with the following code and run it in your browser.

ord(‘9’)) && (ord($str[$i])ord(‘f’)))
return false;

return true;

Encrypt passwords

Total passwords in the table –
0) { ?>
All passwords are encrypted.
0) { ?>
Unencrypted –

Click "GO" to encrypt passwords.
WARNING! There will be no way to decipher the passwords.

PHP: Variables

PHP: Variables

A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. In PHP all the variables begin with a dollar sign "$" and the value can be assignes using the "=" operator. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP parser to recognize the variable as such.

Another important thing in PHP is that all the statements must end with a semicolon ";". In PHP we needn’t have to specify the variable type, as it takes the data type of the assigned value. The contents of a variable can be changed at any time, and so can its type. To declare a variable, you must include it in your script. You can declare a variable and assign it a value in the same statement.

Here is some code creating and assigning values to a couple of variables:

The output is: Have a nice day! My favourite number is 5.

Case Sensitivity

One thing that causes many problems and take hours of finding mistakes is case sensitivity. PHP is case sensitive. Have a look at the following code:

PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.

* PHP variables must start with a letter or underscore "_".
* PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
* Variables with more than one word should be separated with underscores: $my_variable.
* Variables with more than one word can also be distinguished with capitalization: $myVariable.

One important thing to note if you are coming from another programming language there is no size limit for variables.

Variable References

PHP also allows you to do some neat things with variables. It allows you to create aliases for variables, and it also allows you to have variables whose name is a variable. A variable reference, or alias, is a variable assigned to refer to the same information as another variable. To assign an alias to a variable, you use the reference operator, which is an equals sign followed by an ampersand. The following code snippet outputs ‘Have a nice day!’ twice:

Environment Variables

Beyond the variables you declare in your code, PHP has a collection of environment variables, which are system defined variables that are accessible from anywhere inside the PHP code. All of these environment variables are stored by PHP as arrays. Some you can address directly by using the name of the index position as a variable name. Other can only be accessed through their arrays.

Some of the environment variables include:

$_SERVER Contains information about the server and the HTTP connection. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).
$_COOKIE Contains any cookie data sent back to the server from the client. Indexed by cookie name. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).
$_GET Contains any information sent to the server as a search string as part of the URL. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).
$_POST Contains any information sent to the server as a POST style posting from a client form. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).
$_FILE Contains information about any uploaded files. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated).
$_ENV Contains information about environmental variables on the server. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).

The code to use the environment variables will be as follows:

upload images into a mysql database using PHP

Image Databasing
This tutorial teaches you how to upload images into a mysql database using PHP. Even though it sounds complicated, it is fairly simple and has many practical applications. One example of an application would be Forum User Images. Take user avatars for example. Its impractical to upload avatar files to one common folder, because chances are sooner or later two users will have the same name for an avatar, and either one avatar will be overwritten, or the other not accepted, causing trouble. Image Databasing solves this problem by inserting the image data into its own unique row in a table, each assigned with an ID number instead of a filename. Images can then be called from the database and be view using one php file for all images. How are they inserted into the database? By converting the data to base64. If your confused, please bear with me, you will understand it soon.
There will be 3 PHP files in this tutorial:

1. readdir.php – this puts all the images in a folder into the database
2. image.php – the actual image script that displays the imag
3. view.php – an example file that shows you how to call the image

Sponsors – Spoono Host
Creating the Image Database
First, create a mysql database called base64imgdb (this is the name that will be used throughout the tutorial)
Second, create a table called images with two rows. Name the first one imgid, and give it the parameters TYPE: INT EXTRA: auto_increment, and check the circle under Primary. Name the second sixfourdata, and make it TYPE: LONGTEXT. Here is the sql code:

CREATE TABLE `images` (
`sixfourdata` LONGTEXT NOT NULL ,
PRIMARY KEY ( `imgid` )

The READDIR.PHP script
This script reads a directory within the server, selects all the jpg and gif images, encodes them into base64, and uploads them to the database, except in a different order. This is because the script reads each image in a loop, and we would like to keep a constant connection to the mysql database instead of creating multiple ones. Here is the database connection where
need to be changed:

Next we need to open the directory, where
is the directory the readdir.php file is located:

$path = "./";
$dir_handle = opendir($path) or die("Unable to open directory $path");

This is the hardest part of the script: sorting the image types, reading the data using
, converting it using
, and then inserting it into the table.

This is the last and final part of the readdir.php: closing the directory and stating the proccess is complete:

The Image Reader IMAGE.PHP
This file may be the hardest file to understand whenever you see how simple view.php is, but bear with me, your patience will pay off. This file takes a request, requests the row in the table, decodes the data, and presents itself as an image. First, we have to connect to the database again:

Now we need to find out which row its requesting, which is done using

After this, we need to connect to the table, get the data, and set it into variables:

Error performing query: " . mysql_error() . "");
while ($row = mysql_fetch_array($result)) {
$imgid = $row["imgid"];
$encodeddata = $row["sixfourdata"];

Now here is the last and most confusing part of the file:

Now let me explain this. All this does is decodes the base64-encoded image data, end echos it. Thats it, nothing else.
"Let me see the picture already!" VIEW.PHP (example viewer)
Okay, so you made it this far already. This is now the easiest to copy and paste but hardest part to understand, where
matches with whatever row the image is on, for example if its row 357 then you would need to put


Now that wasnt so hard was it? But most of you are probably wondering why when you link to a page, you get an image. This is the reason: images arent defined by their 3 letter suffixes (such as jpg or gif), but by how their headers are written. IMAGE.PHP simply echos the image data, and acts like an image even though it just proccesses the request. This is why you get an image.

The files please!


Error performing query: " . mysql_error() . "");
while ($row = mysql_fetch_array($result) ) {
$imgid = $row["imgid"];
$encodeddata = $row["sixfourdata"];
echo base64_decode($encodeddata);

And view.php (i shouldnt need to post this..)


Browser Redirect

Browser Redirect
Learn how to do a Browser Redirect using the power of PHP.

Sponsors – Spoono Host
Alright, this is a simple little script which will take you to one page if you’re using Internet Explorer, and another page if you’re using some other browser. Lets decide what this is going to do in English:

1. If the browser is MicroSoft Internet Explorer (MSIE), then go to spoono.com
2. If the browser isn’t MSIE, then redirect to yahoo.com.

The most essential part of this is that this code has to be sent out before any output to the HTML page. Make sure it is the first line of code on your PHP page. With that said, here is the PHP:

Its not as hard as it looks in the beginning, is it? Simple and easy to do.

PHP Errors and How to Fix Them

Errors and How to Fix Them
This tutorial will briefly describe some of the common errors in PHP.

Sponsors – Spoono Host
I get a myriad of e-mails from visitors who get a particular error whenever they’re coding, or doing one of the PHP tutorials. Most of the errors which people get are very similar so I thought I’d go through some of the most common errors, what causes them, and how to fix it. The three most common error types are:

1. Parse Errors
2. Header Errors
3. mySQL Result Source Errors

Parse Errors
What Error Says:
Parse error: parse error, unexpected T_STRING in /home/virtual/site5/fst/var/www/html/akash/errors.php on line 11

Cause of Error
A parse error is a runtime error whenever the PHP compiler is trying to compile your code. Its usually something simple, and can usually be fixed fairly easily. The problem is the syntax of your code, perhaps you forgot a semi-colon on one of the lines. Another infamous parse error is forgetting a double quote (") or an end bracket (}) after you started one. In that case, the error will not be on the line where the brace of quote should be, but might be at the end of your program.

How to Fix Them
Well, the easiest way to go about fixing parse errors is to find the line which PHP is saying caused the error. If you notice that you forgot a simple semi-colon, then insert it and you’ll be on your way. If the line it says is the last line of your code, then you need to go back and look through your code to see if all the { } and " are set up properly. One way to check the brackets is to tab them properly and see if they line up. To check for the ", you can try to use a color coded editor, which will usually indicate anything inside a " in a different color.

Header Errors
What Error Says:
Warning: Cannot add header information – headers already sent by (output started at /home/virtual/site5/fst/var/www/html/akash/errors.php:9) in /home/virtual/site5/fst/var/www/html/akash/errors.php on line 10

Cause of Error
HTTP Headers are specific functions defined by PHP which are required to be sent before any output from your script. This means that if it is a header function, then you must place it before any physical output, such as or even a whitespace. Fortunately, there aren’t too many header functions out there, but if you get this error, then I guess you’ve found one. Two of the most important ones are the redirect function in PHP:
Header("location: http://ww.spoono.com");
and the

How to Fix Them
Chances are that if you have a header error, you’re trying to pull a function that’s supposed to be before any other ouput in your file after you’ve placed an output. Go to the line where the error is, select that section and punch it up to the top of your code. Also, if you must have an output before the header function, there is an alternative. Place the code
at the top before any other text, and then place
later on near wherever you must run the header script. That should be about it.

mySQL Result Source Errors
What Error Says:
Warning: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/virtual/site5/fst/var/www/html/tutorials/php/error/index.php on line 3

Cause of Error
Contrary to what the line number might say, this error is sort of tricky because more times than not, the error occurs on the $result variable you use to define the loop. For example say your code looks like the following:

The problem displayed would be with the mysql_fetch_array() function on the while loop. However, the real problem is that the $result variable misspells the word "order" on line 1. The $result variable is not executed till the while loop runs on line 3, which is why the error line is given on that line.

How to Fix Them
The first thing to do when you see the mySQL Resource error is to go to the line which PHP says is causing the problem. Next, make sure that the syntax on the line is right, more times than not it will be. Now, the main thing to check where most of the problem usually hides is inside the $result variable definition. For example, on our code up there, the problem is not on the
where PHP says it is, but rather on the $result variable defined above it. 99.9% of the time, there is a problem with that, whether it be something misspelled or undefined. To find out the particular problem, you can replace your $result with the following code:
, which will display the cause of the error. Go check and fix that and you’ll be on your way.