Creating SEO friendly URL using htaccess

Creating SEO friendly URL requires a knowledge on how htaccess works. Not just that, you should also be familiar with Regular Expressions but I won’t cover that one in this post though since we’ll be focusing on putting those in htaccess. Now, let’s get our hands dirty.

For example:
http://mysite.com/profile.php?username=JuanDelaCruz

If you wanted that one to become
http://mysite.com/user/JuanDelaCruz/

This is what you’re going to do:

#RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-d 
#RewriteCond %{REQUEST_FILENAME}\.php -f 
#RewriteRule ^(.*)$ $1.php

# Use the following rule if you want to make the page like a directory
RewriteCond %{REQUEST_URI} !^/user/profile.php
RewriteRule ^user/(.*[^/])$ user/$1/ [R=301,L]

# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^profile.php$ user/%1?

There are comments, so hope you can understand it from there.
Hope this helps someone! Have a Happy Coding! :)

How to create a zip file in PHP

Howdy!

I will show you how to create a zip file in PHP. All the directory in your server will also be zipped in the code. I will use the¬†ZipArchive class in php. It’s easy as 1 2 3 :)

function ListFiles($dir) {
	if($dh = opendir($dir)) {
		$files = Array();
		$inner_files = Array();
		while($file = readdir($dh)) {
			if($file != "." && $file != ".." && $file[0] != '.') {
				if( $dir == getcwd() )	continue;
				if(is_dir($dir . "/" . $file)) {
					$inner_files = ListFiles($dir . "/" . $file);
					if(is_array($inner_files)) $files = array_merge($files, $inner_files); 
				} else {
					array_push($files, $dir . "/" . $file);
				}
			}
		}

		closedir($dh);
		return $files;
	}
}

$zip = new ZipArchive();
$backupName = date("Y-M-d-h:i:a",time());
$filename = $backupName.".zip";
#Store the backup file in a Folder called 'Files'
if( $zip->open("Files/".$filename,ZIPARCHIVE::CREATE) !== TRUE ) {
	exit("Cannot open the archive file");
}
#Replace this one with your root directory
$directory = '/home/mysite/public_html';
foreach (ListFiles($directory) as $key=>$file){
        #do not include the backup files to be backed-up 
	if( strstr($file,getcwd()) != "" ) continue; 
	$zip->addFile($file);
}  
$zip->close();

To download the zip file, just access it through your browser, so mine is I put the zip file in a File Directory, so it should be something like this: http://mysite.com/File/ . It really depends.

Hope this helps someone! Have a happy coding!

Displaying all database records in a dropdown in Yii

I will show you how to display all database records of a certain table in a dropdownlist in Yii.

Model Code:

class UserType extends CActiveRecord {
     public function GetUserType(){
	$connection=Yii::app()->db; 
	$command= $connection->createCommand("SELECT * FROM ".$this->tableName());
	$rows = $command->queryAll(); 
	return CHtml::listData($rows,'id', 'type');
     }
}

View Code:

echo $form->dropDownList($model,'user_type_id',UserType::model()->GetUserType());

The CHTML::listData() takes 3 parameters.
1st is the result of the row of your sql
2nd parameter is the value of your option. So it’s something like this <option value=”value here“>
3rd parameter is the value between <option>value here</option>

Yii Date validation in rules

Here’s how to specify a date validation rules in the Model.

The idea here is that “Date To” should always be greater than “Date From”. Here’s how to do that

public function rules()
{
   return array(
	array( 'date_to','compare','compareAttribute' => 'date_from','operator'=>'>', 'allowEmpty'=>'false', 'message' => '{attribute} should be greater than "{compareValue}".'),
   );
}

Piece of cake eh?

Have a happy coding! :)

Implementing Paypal Subscription API in PHP

I’m working on implementing Paypal Subscription API in PHP for the past weeks, I thought this was kinda complicated just like the DirectPayment method API. Guess what, it was just a piece of cake, i’ll show you my code:


<form name="_xclick" id="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal - it's fast, free and secure!">
		<input type="hidden" name="cmd" value="_xclick-subscriptions">
		<input type="hidden" name="business" value="yourpaypalaccount@samplemail.com">
		<input type="hidden" name="currency_code" value="USD">
		<input type="hidden" name="no_shipping" value="1">
		<input type="hidden" name="a3" value="20">
		<input type="hidden" name="p3" value="1">
		<input type="hidden" name="t3" value="M">
		<input type="hidden" name="src" value="1">
		<input type="hidden" name="sra" value="1">
		<input type="hidden" name="return" value="http://mysite.com/return.php">
		<input type="hidden" name="cancel_return" value="http://mysite.com/cancel.php">
		<input type="hidden" name="item_name" value="Subscription Fee">
		<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal">
</form>';

Here’s an explanation about that code, it’s pretty straightforward though.
a3 – amount to billed each recurrence
p3 – number of time periods between each recurrence
t3 – time period (D=days, W=weeks, M=months, Y=years)
return – Return URL after the user has paid
Cancel – the URL when the user clicks on Cancel on the Paypal Page

On this example, there will be a Subscription Button being displayed, and the user will have to click on the button so that they will be redirected to paypal’s website.

Subscribe Button

 

 

If you want something like there’s no user interaction, like there would be no user clicking, just use javascript in sending the form. Just like this:

<script>document.forms['_xclick'].submit();</script>

And don’t forget to wrap the form code with an echo. So it should look like this:

echo '<form name="_xclick" id="_xclick" action="https://www.paypal.com/cgi-bin/webscr" method="post" style="display:none;">
		<input type="hidden" name="cmd" value="_xclick-subscriptions">
		<input type="hidden" name="business" value="yourpaypalaccount@samplemail.com">
		<input type="hidden" name="currency_code" value="USD">
		<input type="hidden" name="no_shipping" value="1">
		<input type="hidden" name="a3" value="20">
		<input type="hidden" name="p3" value="1">
		<input type="hidden" name="t3" value="M">
		<input type="hidden" name="src" value="1">
		<input type="hidden" name="sra" value="1">
		<input type="hidden" name="return" value="http://mysite.com/return.php">
		<input type="hidden" name="cancel_return" value="http://mysite.com/cancel.php">
		<input type="hidden" name="item_name" value="Subscription Fee">
		<input type="image" src="http://www.paypal.com/en_US/i/btn/btn_subscribe_LG.gif" border="0" name="submit" alt="Make payments with PayPal">
		</form>';
echo "<script>document.forms['_xclick'].submit();</script>";

Just a piece of cake, right? I hope this helps someone.

Have a happy coding :)

Setup Aweber Email Parser

5ad3izx5tyarjaj I will show you how to setup AWeber Email Parser.
To those of you who do not know what Aweber is, it is an email marketing software that allows you to store email addresses and other important stuffs to people who uses your product or whatever purpose you have.

So my idea here is that everytime a user registers in my website, I will get their email address as well as their name then store it in AWeber.

This is the front end of AWeber

AWeber uses a basic Regular Expression so no need to be rattled ( I was too rattled, so I was forced to learn RegEx :) ).

The default setup of AWeber parse is like this, but I changed it.

Rule 1:: \n[>\s]*Email:\s+(.+?)\n Match:
Store in:
Rule 2:: \n[>\s]*Name:\s+(.+?)\n Match:
Store in:

Looking at the image above(not the table), On the Trigger Rule textinput, change the “aweber” into the email address that you wrote when you created a list. Then change the “myDomain” to your domain name.

Just copy this one as your Rule1:  Name:[ ]*([^\n]+)

Now in your php file:

function SendEmailToAweber( $name, $email_address ) {
      $to = "aweber@elsonsolano.com"; #this is the same email address that you wrote when you created a list
      $subject = "Store Email Address from elsonsolano.com";

     #this is important because we told our regular expression to have a newline above it.
     $message .= "\n";
     $message .= "Name: ".$name;

     $headers = 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
     $headers .= 'From: '.$name." <".$email_address."> \r\n";

     mail( $to, $subject, $message, $headers );
}

Everytime you run this function, it will send an email to AWeber then it will parse it using the configuration tha we set up. Now you are golden! :) So that’s it! That’s how easy to setup AWeber email parser. Hope this helps someone. Have a happy coding :)