Global Variables

This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Tampilkan postingan dengan label HTML. Tampilkan semua postingan
Tampilkan postingan dengan label HTML. Tampilkan semua postingan

Rabu, 30 Maret 2011

10 code snippets for PHP developers


I’ve compiled a small list of some useful code snippets which might help you when writing your PHP scripts…

Email address check

Checks for a valid email address using the php-email-address-validation class.
Source and docs: http://code.google.com/p/php-email-address-validation/
01.include('EmailAddressValidator.php');
02. 
03.$validator = new EmailAddressValidator;
04.if ($validator->check_email_address('test@example.org')) {
05.    // Email address is technically valid
06.}
07.else {
08.    // Email not valid
09.}

Random password generator

PHP password generator is a complete, working random password generation function for PHP. It allows the developer to customize the password: set its length and strength. Just include this function anywhere in your code and then use it.
Source : http://www.webtoolkit.info/php-random-password-generator.html
01.function generatePassword($length=9, $strength=0) {
02.    $vowels = 'aeuy';
03.    $consonants = 'bdghjmnpqrstvz';
04.    if ($strength & 1) {
05.        $consonants .= 'BDGHJLMNPQRSTVWXZ';
06.    }
07.    if ($strength & 2) {
08.        $vowels .= "AEUY";
09.    }
10.    if ($strength & 4) {
11.        $consonants .= '23456789';
12.    }
13.    if ($strength & 8) {
14.        $consonants .= '@#$%';
15.    }
16. 
17.    $password = '';
18.    $alt = time() % 2;
19.    for ($i = 0; $i < $length; $i++) {
20.        if ($alt == 1) {
21.            $password .= $consonants[(rand() % strlen($consonants))];
22.            $alt = 0;
23.        } else {
24.            $password .= $vowels[(rand() % strlen($vowels))];
25.            $alt = 1;
26.        }
27.    }
28.    return $password;
29.}

Get IP address

Returns the real IP address of a visitor, even when connecting via a proxy.
Source : http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html
01.function getRealIpAddr(){
02.    if (!empty($_SERVER['HTTP_CLIENT_IP'])){
03.        //check ip from share internet
04.        $ip = $_SERVER['HTTP_CLIENT_IP'];
05.    }
06.    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
07.        //to check ip is pass from proxy
08.        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
09.    }
10.    else{
11.        $ip = $_SERVER['REMOTE_ADDR'];
12.    }
13.    return $ip;
14.}

XSL transformation

PHP5 version
Source : http://www.tonymarston.net/php-mysql/xsl.html
01.$xp = new XsltProcessor();
02. 
03.// create a DOM document and load the XSL stylesheet
04.$xsl = new DomDocument;
05.$xsl->load('something.xsl');
06. 
07.// import the XSL styelsheet into the XSLT process
08.$xp->importStylesheet($xsl);
09. 
10.// create a DOM document and load the XML datat
11.$xml_doc = new DomDocument;
12.$xml_doc->load('something.xml');
13. 
14.// transform the XML into HTML using the XSL file
15.if ($html = $xp->transformToXML($xml_doc)) {
16.    echo $html;
17.}
18.else {
19.    trigger_error('XSL transformation failed.', E_USER_ERROR);
20.} // if
PHP4 version
01.function xml2html($xmldata, $xsl){
02.   /* $xmldata -> your XML */
03.   /* $xsl -> XSLT file */
04. 
05.   $arguments = array('/_xml' => $xmldata);
06.   $xsltproc = xslt_create();
07.   xslt_set_encoding($xsltproc, 'ISO-8859-1');
08.   $html = xslt_process($xsltproc, $xmldata, $xsl, NULL, $arguments);
09. 
10.   if (empty($html)) {
11.       die('XSLT processing error: '. xslt_error($xsltproc));
12.   }
13.   xslt_free($xsltproc);
14.   return $html;
15.}
16. 
17.echo xml2html('myxmml.xml', 'myxsl.xsl');

Force downloading of a file

Forces a user to download a file, for e.g you have an image but you want the user to download it instead of displaying it in his browser.
01.header("Content-type: application/octet-stream");
02. 
03.// displays progress bar when downloading (credits to Felix ;-))
04.header("Content-Length: " . filesize('myImage.jpg'));
05. 
06.// file name of download file
07.header('Content-Disposition: attachment; filename="myImage.jpg"');
08. 
09.// reads the file on the server
10.readfile('myImage.jpg');

String encoding to prevent harmful code

Web applications face any number of threats; one of them is cross-site scripting and related injection attacks. The Reform library attempts to provide a solid set of functions for encoding output for the most common context targets in web applications (e.g. HTML, XML, JavaScript, etc)
Source : http://phed.org/reform-encoding-library/
1.include('Reform.php');
2.Reform::HtmlEncode('a potentially harmful string');

Sending mail

Using PHPMailer
PHPMailer a powerful email transport class with a big features and small footprint that is simple to use and integrate into your own software.
Source : http://phpmailer.codeworxtech.com/
01.include("class.phpmailer.php");
02.$mail = new PHPMailer();
03.$mail->From = 'noreply@htmlblog.net';
04.$mail->FromName = 'HTML Blog';
05.$mail->Host = 'smtp.site.com';
06.$mail->Mailer = 'smtp';
07.$mail->Subject = 'My Subject';
08.$mail->IsHTML(true);
09.$body = 'Hello<br/>How are you ?';
10.$textBody = 'Hello, how are you ?';
11.$mail->Body = $body;
12.$mail->AltBody = $textBody;
13.$mail->AddAddress('asvin [@] gmail.com');
14.if(!$mail->Send())
15.    echo 'There has been a mail error !';
Using Swift Mailer
Swift Mailer is an alternative to PHPMailer and is a fully OOP library for sending e-mails from PHP websites and applications.
Source : http://swiftmailer.org/
01.// include classes
02.require_once "lib/Swift.php";
03.require_once "lib/Swift/Connection/SMTP.php";
04. 
05.$swift =& new Swift(new Swift_Connection_SMTP("smtp.site.com", 25));
06.$message =& new Swift_Message("My Subject", "Hello<br/>How are you ?", "text/html");
07.if ($swift->send($message, "asvin [@] gmail.com", "noreply@htmlblog.net")){
08.    echo "Message sent";
09.}
10.else{
11.    echo 'There has been a mail error !';
12.}
13. 
14.//It's polite to do this when you're finished
15.$swift->disconnect();

Uploading of files

Using class.upload.php from Colin Verot
Source : http://www.verot.net/php_class_upload.htm
1.$uploadedImage = new Upload($_FILES['uploadImage']);
2. 
3.if ($uploadedImage->uploaded) {
4.    $uploadedImage->Process('myuploads');
5.    if ($uploadedImage->processed) {
6.        echo 'file has been uploaded';
7.    }
8.}

List files in directory

List all files in a directory and return an array.
Source : http://www.laughing-buddha.net/jon/php/dirlist/
01.function dirList ($directory) {
02.    // create an array to hold directory list
03.    $results = array();
04. 
05.    // create a handler for the directory
06.    $handler = opendir($directory);
07. 
08.    // keep going until all files in directory have been read
09.    while ($file = readdir($handler)) {
10. 
11.        // if $file isn't this directory or its parent,
12.        // add it to the results array
13.        if ($file != '.' && $file != '..')
14.            $results[] = $file;
15.    }
16. 
17.    // tidy up: close the handler
18.    closedir($handler);
19. 
20.    // done!
21.    return $results;
22.}

Querying RDBMS with MDB2 (for e.g MySQL)

PEAR MDB2 provides a common API for all supported RDBMS.
Source : http://pear.php.net/package/MDB2
01.// include MDB2 class
02.include('MDB2.php');
03. 
04.// connection info
05.$db =& MDB2::factory('mysql://username:password@host/database');
06.// set fetch mode
07.$db->setFetchMode(MDB2_FETCHMODE_ASSOC);
08. 
09.// querying data
10.$query = 'SELECT id,label FROM myTable';
11.$result = $db->queryAll($query);
12. 
13.// inserting data
14.// prepare statement
15.$statement = $db->prepare('INSERT INTO mytable(id,label) VALUES(?,?)');
16.// our data
17.$sqlData = array($id, $label);
18.// execute
19.$statement->execute($sqlData);
20.$statement->free();
21. 
22.// disconnect from db
23.$db->disconnect();

HTML Tags Chart

Note: For additional tutorials to assist you in HTML and much more, click here to sign up for our free membership club. You will instantly receive a wealth of quality information, such as dozens of video tutorials, several informative ebooks, utilities to assist you in creating buttons, tables, meta tags, and more. Best of all, it's completely free.

To use any of the following HTML tags, simply select the HTML code you'd like and copy and paste it into your web page.
Tag
Name
Code Example
Browser View
<!-- comment <!--This can be viewed in the HTML part of a document--> Nothing will show (Tip)
<a - anchor <a href="http://www.domain.com/">
Visit Our Site</a>
Visit Our Site (Tip)
<b> bold <b>Example</b> Example
<big> big (text) <big>Example</big> Example (Tip)
<body> body of HTML document <body>The content of your HTML page</body> Contents of your web page (Tip)
<br> line break The contents of your page<br>The contents of your page The contents of your web page
The contents of your web page
<center> center <center>This will center your contents</center>
This will center your contents
<dd> definition description <dl>
<dt>Definition Term</dt>
<dd>Definition of the term</dd>
<dt>Definition Term</dt>
<dd>Definition of the term</dd>
</dl>

Definition Term
Definition of the term
Definition Term
Definition of the term
<dl> definition list <dl>
<dt>Definition Term</dt>
<dd>Definition of the term</dd>
<dt>Definition Term</dt>
<dd>Definition of the term</dd>
</dl>

Definition Term
Definition of the term
Definition Term
Definition of the term
<dt> definition term <dl>
<dt>Definition Term</dt>
<dd>Definition of the term</dd>
<dt>Definition Term</dt>
<dd>Definition of the term</dd>
</dl>

Definition Term
Definition of the term
Definition Term
Definition of the term
<em> emphasis This is an <em>Example</em> of using the emphasis tag This is an Example of using the emphasis tag
<embed>
embed object <embed src="yourfile.mid" width="100%" height="60" align="center">
(Tip)
<embed> embed object <embed src="yourfile.mid" autostart="true" hidden="false" loop="false">
<noembed><bgsound src="yourfile.mid" loop="1"></noembed>

&lt;bgsound src="wonderfu.mid" autostart="false" loop="1" /&gt;
Music will begin playing when your page is loaded and will only play one time. A control panel will be displayed to enable your visitors to stop the music.
<font> font <font face="Times New Roman">Example</font> Example (Tip)
<font> font <font face="Times New Roman" size="4">Example</font> Example (Tip)
<font> font <font face="Times New Roman" size="+3" color="#ff0000">Example</font> Example (Tip)
<form> form <form action="mailto:you@yourdomain.com">
Name: <input name="Name" value="" size="10"><br>
Email: <input name="Email" value="" size="10"><br>
<center><input type="submit"></center>
</form>
Name: (Tip)
Email:
<h1>
<h2>
<h3>
<h4>
<h5>
<h6>
heading 1
heading 2
heading 3
heading 4
heading 5
heading 6
<h1>Heading 1 Example</h1>
<h2>Heading 2 Example</h2>
<h3>Heading 3 Example</h3>
<h4>Heading 4 Example</h4>
<h5>Heading 5 Example</h5>
<h6>Heading 6 Example</h6>

<head> heading of HTML document <head>Contains elements describing the document</head> Nothing will show
<hr> horizontal rule <hr />
Contents of your web page (Tip)

Contents of your web page
<hr> horizontal rule <hr width="50%" size="3" /> Contents of your web page

Contents of your web page
<hr> horizontal rule <hr width="50%" size="3" noshade /> Contents of your web page

Contents of your web page
<hr>
(Internet
Explorer)
horizontal rule <hr width="75%" color="#ff0000" size="4" /> Contents of your web page

Contents of your web page
<hr>
(Internet
Explorer)
horizontal rule <hr width="25%" color="#6699ff" size="6" /> Contents of your web page

Contents of your web page
<html> hypertext markup language <html>
<head>
<meta>
<title>Title of your web page</title>
</head>
<body>HTML web page contents
</body>
</html>
Contents of your web page
<i> italic <i>Example</i> Example
<img> image <img src="Earth.gif" width="41" height="41" border="0" alt="text describing the image" /> a sentence about your site (Tip)
<input> input field Example 1:

<form method=post action="/cgi-bin/example.cgi">
<input type="text" size="10" maxlength="30">
<input type="Submit" value="Submit">
</form>
Example 1: (Tip)

<input>
(Internet Explorer)
input field Example 2:

<form method=post action="/cgi-bin/example.cgi">
<input type="text" style="color: #ffffff; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72a4d2;" size="10" maxlength="30">
<input type="Submit" value="Submit">
</form>
Example 2: (Tip)

<input> input field Example 3:

<form method=post action="/cgi-bin/example.cgi">
<table border="0" cellspacing="0" cellpadding="2"><tr><td bgcolor="#8463ff"><input type="text" size="10" maxlength="30"></td><td bgcolor="#8463ff" valign="Middle"> <input type="image" name="submit" src="yourimage.gif"></td></tr> </table>
</form>
Example 3: (Tip)

<input> input field Example 4:

<form method=post action="/cgi-bin/example.cgi">
Enter Your Comments:<br>
<textarea wrap="virtual" name="Comments" rows=3 cols=20 maxlength=100></textarea><br>
<input type="Submit" value="Submit">
<input type="Reset" value="Clear">
</form>
Example 4: (Tip)


<input> input field Example 5:

<form method=post action="/cgi-bin/example.cgi">
<center>
Select an option:
<select>
<option >option 1</option>
<option selected>option 2</option>
<option>option 3</option>
<option>option 4</option>
<option>option 5</option>
<option>option 6</option>
</select><br>
<input type="Submit" value="Submit"></center>
</form>
Example 5: Tip)

Select an option:
<input> input field Example 6:

<form method=post action="/cgi-bin/example.cgi">
Select an option:<br>
<input type="radio" name="option"> Option 1
<input type="radio" name="option" checked> Option 2
<input type="radio" name="option"> Option 3
<br>
<br>
Select an option:<br>
<input type="checkbox" name="selection"> Selection 1
<input type="checkbox" name="selection" checked> Selection 2
<input type="checkbox" name="selection"> Selection 3
<input type="Submit" value="Submit">
</form>
Example 6: (Tip)

Select an option:
Option 1
Option 2
Option 3

Select an option:
Selection 1
Selection 2
Selection 3
<li> list item Example 1:

<menu>
<li type="disc">List item 1</li>
<li type="circle">List item 2</li>
<li type="square">List item 3</li>
</MENU>

Example 2:

<ol type="i">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>
Example 1: (Tip)
  • List item 1
  • List item 2
  • List item 3

Example 2:

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
<link> link <head>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>

<marquee>
(Internet
Explorer)
scrolling text <marquee bgcolor="#cccccc" loop="-1" scrollamount="2" width="100%">Example Marquee</marquee> Example Marquee (Tip)
<menu> menu <menu>
<li type="disc">List item 1</li>
<li type="circle">List item 2</li>
<li type="square">List item 3</li>
</menu>
  • List item 1
  • List item 2
  • List item 3
<meta> meta <meta name="Description" content="Description of your site">
<meta name="keywords" content="keywords describing your site">
Nothing will show (Tip)
<meta> meta <meta HTTP-EQUIV="Refresh" CONTENT="4;URL=http://www.yourdomain.com/"> Nothing will show (Tip)
<meta> meta <meta http-equiv="Pragma" content="no-cache"> Nothing will show (Tip)
<meta> meta <meta name="rating" content="General"> Nothing will show (Tip)
<meta> meta <meta name="robots" content="all"> Nothing will show (Tip)
<meta> meta <meta name="robots" content="noindex,follow"> Nothing will show (Tip)
<ol> ordered list Numbered

<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>

Numbered Special Start

<ol start="5">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>

Lowercase Letters <ol type="a">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>

Capital Letters

<ol type="A">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>

Capital Letters Special Start

<ol type="A" start="3">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>

Lowercase Roman Numerals

<ol type="i">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>

Capital Roman Numerals

<ol type="I">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>

Capital Roman Numerals Special Start
<ol type="I" start="7">
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
<li>List item 4</li>
</ol>
Numbered
  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
Numbered Special Start

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
Lowercase Letters

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
Capital Letters

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
Capital Letters Special Start

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
Lowercase Roman Numerals

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
Capital Roman Numerals

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
Capital Roman Numerals Special Start

  1. List item 1
  2. List item 2
  3. List item 3
  4. List item 4
<option> listbox option <form method=post action="/cgi-bin/example.cgi">
<center>
Select an option:
<select>
<option>option 1</option>
<option selected>option 2</option>
<option>option 3</option>
<option>option 4</option>
<option>option 5</option>
<option>option 6</option>
</select><br>
</center>
</form>
Select an option: (Tip)

<p> paragraph This is an example displaying the use of the paragraph tag. <p> This will create a line break and a space between lines.

Attributes:

Example 1:<br>
<br>
<p align="left">
This is an example<br>
displaying the use<br>
of the paragraph tag.<br>
<br>
Example 2:<br>
<br>
<p align="right">
This is an example<br>
displaying the use<br>
of the paragraph tag.<br>
<br>
Example 3:<br>
<br>
<p align="center">
This is an example<br>
displaying the use<br>
of the paragraph tag.
This is an example displaying the use of the paragraph tag.
This will create a line break and a space between lines.

Attributes:

Example 1:

This is an example
displaying the use
of the paragraph tag.

Example 2:

This is an example
displaying the use
of the paragraph tag.
Example 3:

This is an example
displaying the use
of the paragraph tag.
<small> small (text) <small>Example</small> Example (Tip)
<strike> deleted text <strike>Example</strike> Example
<strong> strong emphasis <strong>Example</strong> Example
<table> table Example 1:

<table border="4" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
</table>

Example 2: (Internet Explorer)

<table border="2" bordercolor="#336699" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
</table>

Example 3:

<table cellpadding="2" cellspacing="2" width="100%">
<tr>
<td bgcolor="#cccccc">Column 1</td>
<td bgcolor="#cccccc">Column 2</td>
</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
</tr>
</table>
Example 1: (Tip)
Column 1 Column 2

Example 2: (Tip)

Column 1 Column 2

Example 3: (Tip)

Column 1 Column 2
Row 2 Row 2
<td> table data <table border="2" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>Column 1</td>
<td>Column 2</td>

</tr>
</table>

Column 1 Column 2
<th> table header <div align="center">
<table>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>

</tr>
<tr>
<td>Row 2</td>
<td>Row 2</td>
<td>Row 2</td>
</tr>
<tr>
<td>Row 3</td>
<td>Row 3</td>
<td>Row 3</td>
</tr>
<tr>
<td>Row 4</td>
<td>Row 4</td>
<td>Row 4</td>
</tr>
</table>
</div>
Column 1 Column 2 Column 3
Row 2 Row 2 Row 2
Row 3 Row 3 Row 3
Row 4 Row 4 Row 4
<title> document title <title>Title of your HTML page</title> Title of your web page will be viewable in the title bar. (Tip)
<tr> table row <table border="2" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td>Column 1</td>
<td>Column 2</td>
</tr>
</table>

Column 1 Column 2
<tt> teletype <tt>Example</tt> Example
<u> underline <u>Example</u> Example
<ul> unordered list Example 1:<br>
<br>
<ul>
<li>List item 1</li>
<li>List item 2</li>
</ul>
<br>
Example 2:<br>
<ul type="disc">
<li>List item 1</li>
<li>List item 2</li>
<ul type="circle">
<li>List item 3</li>
<li>List item 4</li>
</ul>
</ul>
Example 1:

  • List item 1
  • List item 2

Example 2:

  • List item 1
  • List item 2
    • List item 3
    • List item 4
MouseOver PopUps provided by:

Would you like to learn how to design your own web site?
Whether you are a complete beginner or have some web site design experience, this web design course will teach you how to plan, design, build and market your own web site

If you like these codes, you'll love Web Design Mastery. Get these codes and many more.

"I have learned more from this web design course than I did from a course I took in college!" - Joseph Seeles

 More


Web Design Information:

Web Design Articles:

Selecting a Quality Domain Name
Selecting the Best Web Design Language for Your Project
Bring Your Web Site to Life With PHP
The Birth of a Professional Web Site (10 part series)
Increase Your Traffic by Recovering Your Lost Visitors
Using HTML Tables to Format Your Web Page
HTML Forms -- Back to the Basics and Beyond Part One - Basic Forms Tutorial
HTML Forms -- Back to the Basics and Beyond Part Two -  Advanced Forms
HTML Forms -- Back to the Basics and Beyond Part Three - Form Tips & Tricks
35 Deadly Web Site Sins that will Kill Your Business!
Selecting A Quality Web Host
Mini-Sites -- Highly Targeted Sales Generators
Spice Up Your Web Site with JavaScript
Use CGI to Automate Your Web Site
Give Your Graphics A Professional Look without the Price
Use JavaScript to Dynamically Update Your Website
10 Website Essentials to Increase Your Sales
Is Your Domain Name A Trademark Infringement?
Steps to Optimizing Your HTML Codes
The Secrets to Building a Successful Website

Web Development Tutorials:

Back


| Web Site Development | HTML Codes | HTML Tips | Javascript Snippets |
| Web Resources | 216 Safe Colors | Symbols | Web Development Strategies | Web Site Templates |
| Contact Us | About | Privacy Policy | Terms & Conditions | Site MapAdvertise | Affiliate Program |


Web-Source.net http://www.web-source.net