Monthly Archives: June 2009

PHP code to get domain name from a URL

To get the domain name use this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$url = 'http://www.example.com/process.php?act=update&id=33'
 
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i', "http://www.php.net/index.html", $matches);
$host = $matches[1];
 
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {$matches[0]}\n";
 
// host will return: www.example.com
// $matches[0] will return: example.com
?>

To get the script name use this

$_SERVER[‘SCRIPT_NAME’]

// will return: /process.php

and to get the parameters passed, use this

$_SERVER[“QUERY_STRING”]

// will return: act=update&id=33

Remove unnecessary space between lines in dreamweaver

Sometimes code in dreamweaver got space in between. This will make your code very lengthy and hard to read. To remove the space you can use this method in Dreamweaver

1. Open the file (source code)
2. Click CTRL + F
3. Select “Current document” in “Find in” (You can also select the folder if you have multiple files)
4. Search in “Source code”
5. Tick “Use regular expression”
6. Type “[\r\n]{2,}” (without quotes) in “Find”
7. Type “\n” (without quotes) in “Replace”
8. Press “Replace All”

You are done! All the spaces gone.

source

How to randomly read line from a file

This is a very basic way to build a ‘dynamic’ content to your site. You can randomly display quotes, testimonials or what ever content you want with a pre-loaded content in a text file.

Let say you want to display a random quote line from a file.

1
2
3
4
5
6
7
8
<?
function randomLine($filename)
{
    $lines = file($filename) ;
    echo $lines[array_rand($lines)] ;
}
randomLine("quotes.txt"); // example code to call function
?>

Also read how to write to a flat file (to make it even more dynamic – can add content via form)

Use Gmail with your own domain name

Gmail so far is the best known e-mail service provider. I don’t have to list the features of Gmail here.

If you want to have google powered applications (especially Gmail) but using your own domain, you have come to the right place. You can do that with Google Apps (standard edition)

With standard edition (there is premier edition for big corp), you can create 50 email accounts with 7GB space for free and other standard google applications like docs, calendar, chat and google sites.

Below is the steps to start using Gmail with your very own domain name. Very easy.

1. Start here – list of what you get besides gmail. Click on the “Get Started” button

2. Enter your domain name and other details in the provided form.

3. Create admin account and agree with the term

4. Activate gmail and change MX records in cPanel (you can choose from a list of hosting management applications). change MX in cpanel use following values

Priority Mail Server
1 ASPMX.L.GOOGLE.COM.
5 ALT1.ASPMX.L.GOOGLE.COM.
5 ALT2.ASPMX.L.GOOGLE.COM.
10 ASPMX2.GOOGLEMAIL.COM.
10 ASPMX3.GOOGLEMAIL.COM.

source 

At first I thought it was so complicated because I need to change the MX record, then all the A’s records, then CNAME and then other xx records but actually you have to change only MX record and it is in you hosting cPanel (not even in the domain management).

After changing your MX record, you are done. Wait for few hours (sometimes just in few minutes). You can also use all other applications offered as in the intro page.

To log on to your email and other app, just point your browser to

http://www.google.com/a/your_domain_here/
Direct link to email is http://mail.google.com/a/your-domain.com

It is a good idea to make a forwarding or subdomain from your domain to above URL. Easier to remember..

Update: To import all your old emails, you can use mail fetcher by going to Settings > Accounts > Add a mail account you own. Tips for cpanel user, if you are using same email address with same domain, just enter any email address when asked. The important thing is use the real email address in the username. Save and wait while all old emails being fetched.

Hope this article helps. All the best.

For more info, can also check google app setup guide

How to send email using PHP mail() function

Here’s a quick way to send an email. You only need one line for that. I always use this script to send an alert on any event triggered. Let say somebody just make a payment on your site.

$headers = "From: sendername <sender@example.com>\r\n";
$headers .= 'Cc: cc@example.com' . "\r\n";
$headers .= 'Bcc: bcc@example.com' . "\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
mail('recipient@example.com','Subject','Message',$headers);

For the “from” email, some hosting requires you to use only registered email address in your hosting/domain.

How to write to a flat file with PHP

Sometimes writing to a file is much easier that writing to a database. It is quite a tedious job to create a database, create a user to point to a database then create table and so on.

With a flat file you can just create a blank file (with any extension). I prefer still to use .php file so that people cannot read the file via web.

Here is the code to write and append to a file.

1
2
3
4
5
6
7
$name = "anyname"; $email = "anyemail";
$myFile = "yourdatafile.php";
 
$fh = fopen($myFile, 'a') or die("Failed to open file.");
$stringData = "$name;;$email\n";
fwrite($fh, $stringData);
fclose($fh);

Make sure you set your file to 666 (writable). If you want to overwrite each time data is saved, use fopen($myFile, ‘w’) ~ parameter ‘w’ instead of ‘a’

How to display all PHP server variables

Sometimes I get confused with what result a server variables would return.  For example between the $_SERVER[‘SCRIPTNAME’] and $_SERVER[‘SCRIPT_FILENAME’].

Use the following code to display all server variables. From there you can decide which server variable is suitable to use.

1
2
3
4
echo "<table>";
foreach($_SERVER as $key=>$value)
        echo "<tr><td>$key</td><td>$value</td></tr>";
echo "</table>";

Web sales copy checklist

If you want to sell online. Pay attention. You need all the following in your page..

  1. Powerful and attention grabbing headline
  2. One line to summarize top 3 benefits
  3. Establish your credibitly – who are you and why they need to listen to you
  4. Problem & solution – describe the problem, how big can it grow and what solution you have for that problem
  5. About the product – tell more about the product. Always relate the features to benefits
  6. Give examples for them to feel – demo, trial, sample, example
  7. Again – the benefits in bullet form
  8. Testimonials – don’t listen to us, listen to what other people have to say about us
  9. Do comparison to show it is really valuable
  10. Give reason why the price is like that or why you are giving this away with this price
  11. Give bonus – create greedy mentality
  12. Remove barrier. Offer guarantee, money back
  13. Very clear and easy to order – 1-3 steps
  14. Add the P.S – last chance to persuade

Add few “order here” link on the page – after testimonials, benefits, why the price so low, bonus, p.s.