PHP URL rewrite-htacces

30th Jan 2010 at 1:01 PM | Posted in PHP, URL Rewriting | 4 Comments

By rewriting url we can change the original url. We can hide the extension of page. It is  also named as SEO friendly URL.

Before Start rewrite URL:

For rewrite the URL you must have the mod_rewrite module loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?


1. Type <?php phpinfo(); ?> in a php file and save it and run that file in the server. Run tis page.

2. If mod_rewrite is found under the “Loaded Modules” section then this module is already loaded as you see in the picture above, otherwise you need to go to the next step for enabling mod_rewrite module.

If not active then active it:

1.  Find the “httpd.conf” file under the “conf” folder inside the apache’s installation folder.

2. Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.
3. Remove the “#” at the starting of the line, “#” rpresents that line is commented.
4. Now restart the apache server. You can see now “mod_rewrite” in the Loaded Module section while doing “phpinfo()”.

Now a simple script:
1. Create a folder “url_rewrite”. Inside this we will create our files.
2. Create a file test.php:

<html>
<body>
This is test webpage
</body>
</html>

3. Create a .htaccess file. If you can’t create make a file create_htaccess.php.
<?php
$url=$_SERVER['REQUEST_URI'];
$url=explode(“/”,$url);
$final_url=”;
for($i=0;$i<(count($url)-1);$i++)
{
$final_url.=$url[$i].”/”;
}
if(!file_exists(“.htaccess”))
{
if($filename=fopen(“.htaccess”,”w”))
{
echo “.htaccess file created.<br/>Path: “.$final_url;
fwrite($filename,”Options +FollowSymLinks
RewriteEngine on”);
}
fclose($filename);
}
else
echo “.htaccess file already exists in: “.$final_url;

?>

4. In the .htaccess file write code  below:
Options +FollowSymLinks
RewriteEngine on

#give your base url path
RewriteBase /sourav/url_rewrite/

RewriteRule ^(.*)\.htm$ $1.php [nc]

[ Explain: In this we rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.]

5.     Now run the test.php file in browser or
run the test.html file in browser the result is same that is opening the test.php file.
Here we overwriting the extension .php by .html.

Now litle complex script:
1.Create a file “common_function.php” your url_rewrite folder.

<?php
function rewrite($val,$id)
{
if($val==”products”)
return “product-”.$id.”.html”;

if($val==”product_details”)
return “product_details-”.$id.”.html”;
}
?>
2. Create another file “products.php”.
<?php
echo “This is products.php?id=”.$_REQUEST['id'];
?>
3. Create another file “product_details.php”.
<?php
echo “This is product_details.php?product_id=”.$_REQUEST['id'];
?>
4. Include the “common_function.php” file in your test.php page.
<?php
include_once(“common_function.php”);
?>
This is test webpage<br/>
go to <a href=”<?php echo rewrite(“products”,”5″); ?>”>products.php?id=5</a>
go to <a href=”<?php echo rewrite(“product_details”,”3″); ?>”>product_details.php?product_id=3</a>

5. Now change your .htaccess file:
Options +FollowSymLinks
RewriteEngine on

#give your base url path
RewriteBase /sourav/url_rewrite/
RewriteRule ^(.*)\.htm$ $1.php [nc]
#This is for product page
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1

#This is for product_details page
RewriteRule ^product_details-([0-9]+)\.html$ product_details.php?id=$1

6. Now run test.php file or test.html file and click on the links on this page.
see the links are changed.

2. If it is found under the “Loaded Modules” section then this module is already loaded as you see in the picture                below, otherwise you need to go to the next step for enabling mod_rewrite module.

PHP dynamic bar chart

30th Jan 2010 at 5:18 AM | Posted in PHP, PHP pie/bar chart | Leave a comment

<?php
header(“Content-type: image/jpeg”);
$data = array(’3400′,’2570′,’245′,’473′,’1000′,’3456′,’780′);
$sum = array_sum($data);

$height = 255;
$width = 320;
$im = imagecreate($width,$height); // create width , height px of chart background
/*    $background_color = imagecolorallocate($im, 0, 0, 0);
*/    $white = imagecolorallocate($im,255,255,255);
$black = imagecolorallocate($im,0,0,0);
$red = imagecolorallocate($im,255,0,0);
//create the X & Y axis lines.
imageline($im, 10, 5, 10, 230, $black);
imageline($im, 10, 230, 300, 230, $black);

$x = 15;
$y = 230;
$x_width = 20;
$y_ht = 0;

for ($i=0;$i<7;$i++){
$y_ht = ($data[$i]/$sum)* $height;
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);

/*    $colorHandle = imageColorAllocate($im, 225, 192, $i); // allocate color
imageFilledRectangle($im, $x,$y,$x+$x_width,($y-$y_ht), $colorHandle);// use it for drawing
*/
imagestring( $im,2,$x-1,$y+10,$data[$i],$black);
$x += ($x_width+20);
}
imagejpeg($im);
?>

More about imagerectangle here :
http://php.net/manual/en/function.imagerectangle.php

PHP pie chart

30th Jan 2010 at 5:16 AM | Posted in PHP, PHP pie/bar chart | Leave a comment

<?php
$myImage = ImageCreate(300,300);
$white = ImageColorAllocate ($myImage, 255, 255, 255);
$red  = ImageColorAllocate ($myImage, 255, 0, 0);
$green = ImageColorAllocate ($myImage, 0, 255, 0);
$blue = ImageColorAllocate ($myImage, 0, 0, 255);
$lt_red = ImageColorAllocate($myImage, 255, 150, 150);
$lt_green = ImageColorAllocate($myImage, 150, 255, 150);
$lt_blue = ImageColorAllocate($myImage, 150, 150, 255);

for ($i = 120;$i > 100;$i–) {
ImageFilledArc ($myImage, 100, $i, 200, 150, 0, 90, $lt_red, IMG_ARC_PIE);
ImageFilledArc ($myImage, 100, $i, 200, 150, 90, 180, $lt_green, IMG_ARC_PIE);
ImageFilledArc ($myImage, 100, $i, 200, 150, 180, 360, $lt_blue, IMG_ARC_PIE);
}

ImageFilledArc($myImage, 100, 100, 200, 150, 0, 90, $red, IMG_ARC_PIE);
ImageFilledArc($myImage, 100, 100, 200, 150, 90, 180 , $green, IMG_ARC_PIE);
ImageFilledArc($myImage, 100, 100, 200, 150, 180, 360 , $blue, IMG_ARC_PIE);
header (“Content-type: image/png”);
ImagePNG($myImage);
ImageDestroy($myImage);
?>

jquery prev()

28th Jan 2010 at 2:33 PM | Posted in Jquery | Leave a comment

<script>
//cresting an array of colors
var $color_arr= new Array(“#000000″,”green”,”blue”,”red”);
var $i=0;
var col=”;
$(document).ready(function(){
var $curr = $(“#start”);
$curr.css(“background”, “#f99″);
$(“button”).click(function () {
/*code when controls comes to 1st div stsrts */
if($curr.attr(‘id’)==1) //when controles comes to the first div
$curr=$(‘#8′).next();//locate next of last div to the $curr
// use $curr=$(‘#8′); and see the changes.
/*code when controls comes to 1st div ends */

$curr = $curr.prev();
$(“div”).css(“background”, “”);
$curr.css(“background-color”, $color_arr[$i]);
$i++;
if($i>=$color_arr.length) // for rotate colors
$i=0;
});

});
</script>
<style>
div { width:40px; height:40px; margin:10px;
float:left; border:2px blue solid;
padding:2px; }
span { font-size:14px; }
p { clear:left; margin:10px; }
</style>
</head>
<body>
<div id=”1″></div>
<div id=”2″></div>
<div id=”3″></div>
<div id=”4″></div>
<div id=”5″></div>
<div id=”6″></div>
<div id=”start”></div>
<div id=”8″></div>
<p><button>Go to Prev</button></p>
</body>
</html>

Ajax with prototype

25th Jan 2010 at 5:39 AM | Posted in Ajax with jquery & prototype | Leave a comment
var url = 'checkZip.php';
var params = 'zip=' + $F('zip'); //$F('zip')get the value of field zip
var ajax = new Ajax.Updater({success: 'zipResult'},
url,
{method: 'get', parameters: params, onFailure: reportError});
}
}
function reportError(request) {
$F('zipResult') = "Error";
}

The html is below:

<body>
<form>
<label for="zip">zip:</label>
<input type="text" name="zip" id="zip" onkeyup="checkZip();" />
<div id="zipResult"></div>
</form>
</body>

Get help from http://www.petefreitag.com/item/515.cfm

Call ajax using jquery

25th Jan 2010 at 3:33 AM | Posted in Ajax with jquery & prototype | Leave a comment

Save some data to the server and notify the user once it’s complete.

Syntaxt is:

$.ajax({
   type: "POST",
   url: "some.php",//page where tha ajax call is made
   data: "name=John&location=Boston", //this data will be sent to some.php page
   success: function(msg){
     alert( "Data Saved: " + msg );
   }
 });
(don't forget to include jquery.js file into your script)

AJAX Introduction

12th Jan 2010 at 1:54 PM | Posted in AJAX LEARNING | 1 Comment

AJAX = Asynchronous JavaScript and XML.
AJAX is based on JavaScript and HTTP requests.
AJAX is not a new programming language, but a new way to use existing standards.
With AJAX, a JavaScript can communicate directly with the server, with the XMLHttpRequest object. With this object, a JavaScript can trade data with a web server, without reloading the page.

Internet-applications have many benefits over desktop applications; they can reach a larger audience, they are easier to install and support, and easier to develop.
However, Internet-applications are not always as “rich” and user-friendly as traditional desktop applications.
With AJAX, Internet applications can be made richer and more user-friendly.

AJAX uses the XMLHttpRequest object

To get or send information from/to a database or a file on the server with traditional JavaScript, you will have to make an HTML form, and a user will have to click the “Submit” button to send/get the information, wait for the server to respond, then a new page will load with the results. Because the server returns a new page each time the user submits input, traditional web applications can run slowly and tend to be less user-friendly.

With AJAX, your JavaScript communicates directly with the server, through the JavaScript XMLHttpRequest object.

My 1st ajax script

12th Jan 2010 at 1:50 PM | Posted in AJAX LEARNING | Leave a comment

/************ starts ***************/
1st create a div:
<div id=”test”>
<h2>Click to let AJAX change this text</h2>
</div>
Add two buttons &  add a onclick function.
<button type=”button” onclick=”loadXMLDoc(‘test1.txt’)”>Click Me</button>
<button type=”button” onclick=”loadXMLDoc(‘test2.txt’)”>Click Me</button>

Finally place the script into head section.
<script type=”text/javascript”>
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{document.getElementById(‘test’).innerHTML=xmlhttp.responseText}
}
xmlhttp.open(“GET”,url,false);
xmlhttp.send(null);
document.getElementById(‘test’).innerHTML=xmlhttp.responseText;
}
</script>
Now you need two .txt file.
create “test1.txt” &amp; “test2.txt” &amp; write any thing in those page. Save them in same directory where the script is saved.
/*********** ends *********************/

Ajax responseText

12th Jan 2010 at 1:34 PM | Posted in AJAX LEARNING | Leave a comment

Sending an AJAX Request to a Server(Methodes open()/send()):
To send a request to a web server, we use the open() and send() methods.
The open() method takes three arguments. The first argument defines which method to use (GET or POST). The second argument specifies the name of the server resource (URL). The third argument specifies if the request should be handled asynchronously.
The send() method sends the request off to the server. If we assume that requested a file called “time.asp”, the code would be:

Get the responsetext by xmlhttp.responseText &amp; put this in the div whose id is test.

By changing the third parameter in the open call to “true”, you tell the XMLHttpRequest object to continue the execution after the request to the server has been sent.
Because you cannot simply start using the response from the server request before you are sure the request has been completed, you need to set the onreadystatechange property of the XMLHttpRequest, to a function (or name of a function) to be executed after completion.
In this onreadystatechange function you must test the readyState property before you can use the result of the server call.

State Description
0 The request is not initialized
1 The request has been set up
2 The request has been sent
3 The request is in process
4 The request is complete

AJAX Suggest

12th Jan 2010 at 1:18 PM | Posted in AJAX LEARNING | 2 Comments

http://www.w3schools.com/ajax/ajax_source.asp

Next Page »

Blog at WordPress.com. | Theme: Pool by Borja Fernandez.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.