Posts Tagged PHP

XAMPP PHP cURL

cURL (Client for URLs) is in the XAMPP bundle. But you must enable it first before you can use it in your PHP code.

To enable curl library with XAMPP we need to modify the php.ini files in our xampp folder.

1. Locate some or all of the following files:
C:\Program Files\xampp\apache\bin\php.ini
C:\Program Files\xampp\php\php.ini
C:\Program Files\xampp\php\php4\php.ini

2. Uncomment the following line on your php.ini file by removing the semicolon.

;extension=php_curl.dll

3. Restart your apache server.

4. Check your phpinfo if curl was properly enabled.

,

1 Comment

Make Ubuntu PHP Localhost Mail Function Work

This article teaches you how to make Ubuntu PHP localhost mail function work, using PHP to send mail from localhost is easy. First, you need to install some PEAR mail packages.

In terminal, run the following command one by one:
sudo pear install mail
sudo pear install Net_SMTP
sudo pear Auth_SASL
sudo pear install mail_mime

After installing the pear packages, you need to install and configure postfix, first, run the following command to install postfix:

sudo apt-get install postfix

after installation, a configuration window will be displayed, you need to configure it:

Step 1: Choose Internet Site

Ubuntu Postfix

Ubuntu Postfix

Step 2: Enter localhost at the input area

postfix 2

postfix 2

For the rest of the steps, just follow the default settings. After complete the configuration, it’s time to test the PHP script. Create a PHP file called sendmail.php at your localhost, and copy and paste the code below into it (remember to change the email addresses to yours)

<?
include('Mail.php');
include('Mail/mime.php');
// Constructing the email
$sender = "shi <shichuanr@msn.com>";
$recipient = "Leigh <shichuanr@gmail.com>";
$subject = "Test Email";
$text = 'This is a text message.';
$html = '<html><body><p>This is a html message</p></body></html>';
$crlf = "\n";
$headers = array(
'From'          => $sender,
'Return-Path'   => $sender,
'Subject'       => $subject
);
// Creating the Mime message
$mime = new Mail_mime($crlf);
// Setting the body of the email
$mime->setTXTBody($text);
$mime->setHTMLBody($html);
 // Set body and headers ready for base mail class
$body = $mime->get();
$headers = $mime->headers($headers);
// SMTP params
$smtp_params["host"] = "localhost"; // SMTP host
$smtp_params["port"] = "25";               // SMTP Port (usually 25)
// Sending the email using smtp
$mail =&amp;amp; Mail::factory("smtp", $smtp_params);
$result = $mail->send($recipient, $headers, $body);
if($result == 1)
{
echo("Your message has been sent!");
}
else
{
echo("Your message was not sent: " . $result);
}
?>

Now you should be able to receive the mail sent from your localhost. Good Luck! :-)

,

19 Comments

PHP Find Absolute Path

This article teaches you how to find absolute path using PHP. Open your notepad or any other text editor and put in the following PHP script:

<?php
$p = getcwd();
echo $p;
?>

Save the file as path.php (or first as txt and rename to info.php) and upload it to the root directory of your site through ftp. Run it as http://yoursite.com/path.php and you have your absolute path.

Hope this helps! :-)

No Comments

WordPress Mail Localhost

If you develop WordPress locally and try to use the mail function, you may encounter problem. The PHP mail function cannot send mail out. To solve the problem, you can install a free local mail server on your computer, and test it locally.

First, read my another post here about how to install the local mail server.

After that, you can go to WordPress Admin area and change some of the user email to xxx@example.com, and you should be able to send mail in WordPress.

Hope this helps!

No Comments

PHP cURL XAMPP

To install, enable PHP cURL on XAMPP is really easy. All you have to do is to edit php.ini file, there are three php.ini files in your xampp folder, they are:

C:\Program Files\xampp\apache\bin\php.ini
C:\Program Files\xampp\php\php.ini
C:\Program Files\xampp\php\php4\php.ini

You really only have to edit one, which is C:\Program Files\xampp\apache\bin\php.ini.

1. Open the file in your text editor, and search for the line: ;extension=php_curl.dll

2. Uncomment it by removing the semicolon, so it should look like: extension=php_curl.dll

3. Save the file.

4. Restart your Apache Server.

Now you have cURL enabled.

1 Comment

Use Adsense for Search on WordPress

I have searched a few blogs in the hope to find a good way to use Adsense for Search to replace default WordPress search. All the solutions suck, they either ask users to add redundant plugins that doesn’t do the job at all or ways to create a static page to display search results. I got really fed up with these ‘solutions’ and finally decide to make my own easy to implement method.

1. Create the Adsense for Search unit from adsense website (I don’t wanna write the steps here because Google has been doing a great job providing step by step guide to create the search unit).

2. When you complete the steps to create the unit on Adsense, you shall get two parts of the generated code, Search Box Code and Search Results Code, copy the Search Results Code.

3. Go to your blog admin, Create a Page called Search Results,

4. Paste the Search Results Code you just copied into the textarea.

5. Get the page id, the page id is the digits at the end of your edit page url, for instance, if the page editing url you are creating your Search Result page is http://www.yourblog.com/wp-admin/page.php?action=edit&post=739, then your post id is 739.

6. Publish the page.

7. Go to Design->Theme Editor, from the template pages, select Header.php, and search for the line similar to the one below:

wp_list_pages(‘title_li=&depth=1′);

8. Change the line above to the one below and save the file:

wp_list_pages(‘title_li=&depth=1&exclude=739′);

739 is a generic number, it’s the Search Page Result id.

9. Open your template file Sidebar.php, at the place you want the Search Box to appear, paste the Search Results Code at the area. And save the post.

10. Now you are done! It requires neither a static page (in the dynamic world, why need a stupid static page?) nor plugins. Just the simple implementation above! Hope this helps :)

1 Comment

Troubleshoot Flash PHP Can’t Upload Flv

If you try to upload Flash flv file using PHP, and the files are not uploaded, you may even have tried to edit php.ini file, and it still doesn’t work. The reason is simple, for most people who use xampp, the php.ini is not in php/php.ini or php/php5.ini. The file is inside apache/php.ini.

The easiest way to locate the right php.ini file is to write an php file with the code: phpinfo();

View the php file from your browser and search to find the word: php.ini. You should get the path now.

Open the php.ini file and search for the word upload_max_filesize and post_max_size, assign them with bigger values. For instance:

upload_max_filesize = 100M
post_max_size = 100M

If you have tried to increase the upload_max_filesize and post_max_size in the php.ini file and it still doesn’t work, don’t give up. The problem could lies somewhere else, it could because of directory permission.

Good luck with your uploading!

3 Comments

PHP – Display all jpg Image Infomation

Use the following PHP function, you can display all image information (EXIF data) for a particular jpg file.

<?php
echo "aladdin-sane.jpg:<br />\n";
$exif = exif_read_data('david-bowie/aladdin-sane.jpg', 'IFD0');
echo $exif===false ? "No header data found.<br />\n" : "Image contains headers<br />\n";

$exif = exif_read_data('david-bowie/aladdin-sane.jpg', 0, true);
echo "test2.jpg:<br />\n";
foreach ($exif as $key => $section) {
foreach ($section as $name => $val) {
echo "$key.$name: $val<br />\n";
}
}
?> 

Hope it helps!

2 Comments

PHP Random Password Generation

A random password generator is a fairly common function used by PHP coded websites. The one below is what i use for many websites.

<?php
/**
* Random Password Generator
* @public
* @param int $length Length of password to generate
* @return string Returns randomised password
*/
function randomPword($length)
{
$seed = 'ABC123DEF456GHI789JKL0MNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
$seedLength = strlen($seed);
$random = '';
for ($i=1; $i <= $length; $i++)
{
$charPos = mt_rand(0, ($seedLength - 1));
$singleChar = substr($seed, $charPos, 1);
$random .= $singleChar;
}
return $random;
}
?>

Hope it helps! Enjoy!

1 Comment

Backup Export MySQL Database Using PHP

For people who don’t have phpMyAdmin or MySQL Administrator. There is a quick and easy way to backup mysql database using PHP from browser. Below is the code, follow the command you will know how to customize and make it work in your case. Enjoy!

<?php
// Enter your MySQL access data
$host= 'MySQL host';
$user= 'database username';
$pass= 'database password';
$db=   'database name';
// Enter the directory the sql file will be created at.
// For example, if you have a web site www.example.com,
// and you want the sql file to be generated at
// www.example.com/backups, then $backupdir = 'backups';
$backupdir = 'backups';
// Compute the time the sql file's generated.
$today = getdate();
$day = $today[mday];
if ($day < 10) {
$day = "0$day";
}
$month = $today[mon];
if ($month < 10) {
$month = "0$month";
}
$year = $today[year];
$hour = $today[hours];
$min = $today[minutes];
$sec = "00";
// Execute mysqldump command.
// It will produce a file named $db-$year$month$day-$hour$min.gz
// under $DOCUMENT_ROOT/$backupdir
system(sprintf(
'mysqldump --opt -h %s -u %s -p%s %s | gzip > %s/%s/%s-%s%s%s-%s%s.gz',
$host,
$user,
$pass,
$db,
getenv('DOCUMENT_ROOT'),
$backupdir,
$db,
$year,
$month,
$day,
$hour,
$min
));
echo '+DONE';
?>

,

1 Comment