Embedd Javascript in AJAX Response

Hi everyone…

I came across a problem few days back, it was about calling or embedding javascript result/response in AJAX response. I was working on one of the feature of PHP based site http://www.PingMyCompany.com

I found that, javascript can’t be called in AJAX response, possible reason being, AJAX response is build on server side while javascript is simply client side (I may be wrong here, OR there can be some other reason for this as well).

So what to do, what if we have to embed the output of some external JS file in AJAX response as below???

/****AJAX response START****/

/**********Some Response*******************/
/********** Some Response ******************/
/**********Response from some JS file*********/
<script src=”http://www.crunchbase.com/cbw/company/zigron.js&#8221; type=”text/javascript”>
/**********Some Response*******************/
/********** Some Response ******************/

/****AJAX response END****/

Now the response from JS file will not get embedded with AJAX response. So what I did to solve this issue is that I got the output of JS file in a PHP variable using file_get_content function as follows.

$contents = file_get_contents (“http://www.crunchbase.com/cbw/company/zigron.js&#8221;);

Alternatively you can do the same using cURL but file_get_content is really smart solution (if the JS is file is not suspicious…).

Once done, you must know the format of contents being returned by the JS file. Now you can do simple string manipulation on the contents to mangle it according to the requirements and just echo it where you want to embed it in AJAX response. I have removed document.write from the start of the string and escaped few of whitespace characters and that’s it….

****AJAX response START****

/**********Some Response*******************/
/********** Some Response ******************/
/**********Response from some JS file*********/
$content = file_get_contents (“http://www.crunchbase.com/cbw/company/zigron.js&#8221;);
$content = substr($content,16);
$content = substr($content,0, strlen ($content)-2);
$content = str_replace(“\\n”,””,$content);
$content = str_replace(“\\t”,””,$content);
$content = str_replace(‘\\”‘,””,$content);
echo $content;
/**********Some Response*******************/
/********** Some Response ******************/

/****AJAX response END****/

You can see the working example by clicking here. Click on “CrunchBase Profile” tab button which show ajax response built using remote JS file.

Feel free to discuss..


Keep Smiling 🙂

Posted in PHP. Tags: , , . 5 Comments »

Creating Thumbnails in PHP

Hi Everyone…!!!

Before starting make sure that the following line is uncommented in php.ini file


Following is the function which creates 3 copies of image of different size i.e., thumbnail (approx 100*100), small (approx 250*250) and medium (approx 400*400).

You just have to pass it the complete path of the original image and it will create three different images for you.

function CreateThumbnail ($imagePath)
//Create your custom size and names
$sizes = Array (100, 250, 400);
$names = Array (“thumbnail”, “small”,”medium”);

//if passed image exists or not
if (file_exists ($imagePath))
//extract Image name and path
$imageName = substr (strrchr($imagePath, “/”), 1);
$imagePath = substr ($imagePath, 0, strripos ($imagePath,”/”)+1);

//create all my custom images
for ($i=0; $i<count($sizes); $i++)

//Get the dimensions of original image
$sourceImage = imagecreatefromjpeg(“$imagePath”.”$imageName”);
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);

//Dimensions of target image (one by one)
$newWidth = $newHeight = $sizes[$i];

//Create Target image with target dimensions
$targetImage = ImageCreateTrueColor ($newWidth, $newHeight);

//Shrink the image to target image and create it physically

$newHeight,$sourceWidth, $sourceHeight);

imagejpeg($targetImage, “$imagePath”.$names[$i].”_$imageName”);
return true;
return false;

A simple call to this function could be

CreateThumbnail (“images/example.jpg”);
It will create three file namely, thumbnail_example.jpg, small_example.jpg, medium_example.jpg

Keep Smiling 🙂

Posted in PHP. Tags: , . Leave a Comment »

How to run Apache & IIS parallel

Hi everyone…

Couple of months back, I was working on a ASP.NET 1.1 application which requires IIS. The application basically fetches  records from webservice and validating those records with our database.

Meanwhile I was working on an php application and it requires Apache. I was using WAMP5 ver 1.6.6 with apache 2.0.59.

Initially I have to stop IIS in order to use Apache and vice versa. As both of servers uses port 80 for all http request.

At that point, I was aware of that there must be some parameters in php configurations files which would allow me to change the http port from 80 to something else, so I just started browsing configuration files and was able to change the port from 80 to say 8080 and now both of my webservers were happy:

Now I can access my sites like;

for IIS,


for Apache


so for php my localhost is now localhost:8080 and rest is the same. 🙂

What I did:

1. Opened the httpd.conf file
2. Searched for term 80
3. Found on first hit under parameter listen
4. Changed 80 to 8080 there
5. Restarted all wamp services. And thats it.

So rather then searching for 80, you must search for listen parameter and change it

No other changes and settings required. 🙂

Keep Smiling

Getting Last Visited Page in PHP/JS

It is very common that we browse through no of pages and each visited page has the button which takes the user back to previous link.

Browser’s Back button is good option with static pages but for dynamic pages it doesn’t refreshes the page. So the better option is to

1. Either save the previous link and set that value to action of back button OR

2. Use the Javascript function “window.back()” which returns the last visited address.

th of these techniques not only takes you to the last visited link but will also refreshes page.


Keep Smiling 🙂

Increase PHP File Upload Size Limit

Hi Everyone…

I was working on final year project which was web based file manager. In that I was writing script to upload files. The problem i faced was that I can only

upload files of size <= 2MB. I changed the max file upload size in php.ini but it was useless and after lot of search i came to know about .htaccess and it solved my problem. Let me explain this step by step.

1. First of all make sure that your file upload controls allows you to upload files of you requirement. You can change the below highlighted value according to your requirment.

<form enctype=”multipart/form-data” action=”uploader.php” method=”POST”>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”100000” />
Choose a file to upload: <input name=”uploadedfile” type=”file” /><br />
<input type=”submit” value=”Upload File” />

2. By default you can upload a file of max size 2M. To increase this open the file php.ini and look for the following lines.

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

and change the ‘2M’ to your required max size. Restart your apache server and go ahead.

3. If you don’t have access to php.ini file (may be because of shared webserver) or if above step doesn’t work then look for a file .htaccess and find the following lines.

RewriteEngine On
php_value upload_max_filesize 2M

again change the ‘2M’ to your required max size. Restart you apache server try your luck now though I was lucky after this step.. 😉

For very basic tutorial on file uploading see http://www.tizag.com/phpT/fileupload.php

Keep Smiling 🙂

Get Directory size Recursively in PHP

Hi everyone…

Following is the function to get the size of directory whose path is passed to this function. This function recursively finds the size of all the subdirectories and files.

function GetDirSize ($directoryPath)
$size = 0;
if (is_dir($directoryPath))
if ($dh = opendir($directoryPath))
while (($file = readdir($dh)) != false)
if($file != “.” && $file != “..”)
if(is_dir($directoryPath .$delimeter. $file))
$size += GetDirSize ($directoryPath.$delimeter.$file);
$size += filesize($directoryPath.$delimeter.$file);
$size += filesize($directoryPath);
return $size;
Keep Smiling 🙂