Get Latest Updated Data using SSE(Server-Sent-Events) Automatically

hii guys,

This is very interesting topic which i gonna share with you people.  So many times i think about how facebook/Twitter updates, stock price updates, news feeds, sport results are working?

finally i found easy and simple way to do same thing. so i just developed simple application which demonstrate usage of SSE (Sever-Sent-Events).

so i developed Latest Job Post Application, which give you latest job posted by any employee on webpage automatically.

let’s go for coding

index.html

index.php_SSE

so here i’m using SSE and in EventSource i’m passing viewpost.php file, which fetch latest record from table.

viewpost.php

viewpost.php_SSE

As a output in {topjob} div, always latest job will be printed without refreshing page.

About SSE:

A server-sent event is when a web page automatically gets updates from a server.

This was also possible before, but the web page would have to ask if any updates were available. With server-sent events, the updates come automatically.

Examples: Facebook/Twitter updates, stock price updates, news feeds, sport results, etc.

Disadvantage:

IE does not support it .. 😦

Done!

Hope you gonna like Dis. .. 🙂

Creating Excel file using PHP and downloading excel file

Hello guys,

I got easy way to create excel file using PHP and also it will download on client pc. so let’s go for coding.

Put this two lines in top of your PHP file.

header('Content-type: application/excel');
header('Content-Disposition: attachment; filename="sample.xls"');

Content Disposition header to supply a recommended filename and force the browser to display the save dialog.

now in html body part you can put any content which you want to fill it inside excel file.

Done!

List all uploaded file with Delete option

Hello guys,

Today, one of my friend asked me to display the uploaded files from folder with delete option. so i write below code, which i gonna share with u people.

list_file_with_del_option

so here, i’m creating delete button for each file with unique id(filename), when user press on button i’m passing id(filename) to del() function.
so in javascript, del() function i’m getting filename and that name i’m sending to delete.php file through ajax post method.

now let’s have a look on delete.php

delete

in this file, i’m checking whether $fname is file-name or directory-name. if it is file name then i’m deleting that particular file using unlink() function otherwise one error message will display to end user.

Note:  you must have test folder in your current directory if you are following this same code. otherwise you can change name of your whatever folder-name.

hope you all like this !

🙂

Avoiding the PHP GIF Security Issue

PHP GIF security Issue
sometime hacker can add php-code in the middle of GIF image. and they can hack some confidential information from your site.
usually uploading files are moved to given directory. so in form of image hacker will send some code in that directory and if site serves images directory from that directory, then  your site may be open for security exploits.

for ex, let’s say hacker upload an image named test.gif.php the image may be moved to the images directory.if webserver is configured as usaual to process request with .php file, and the site server the images with below url, the request will execute the php code inside that image file.
http://www.yoursite.com/test.gif.php

how to avoid?

basic solution to avoid this problem is to protect upload directory to avoid direct request to where images are located.

so you can fix it by editing .htaccess file.

code
<Files images>
deny from all
</Files>

and alternative solution is add php function to check valid file(image) extension.
please visit the previous post to learn that code.
https://webdevsurya.wordpress.com/2013/01/09/image-upload-validation-using-php/

have a nice DaY 🙂

Playing with Dates

while developing e-commerce project i got requirement like if any user purchase free trail product our website send one link to him which is limited for 3 days,so after 3 days if user click on that link it will show a message like “Your link has been expire”.

so first thing we need to create future date. below function help to create future date.

$limit_date = mktime(0,0,0,date("m"),date("d")+3,date("Y"));

next thing we need to think is how to compare two dates?

we can not directly use comparison operator for dates, so we need to use bellow function

The strtotime() function parses an English textual date or time into a Unix time stamp.

$cdate = date("Y-m-d");
$d1 = strtotime($cdate);
$d2 = strtotime($limitdate);
if($d1 > $d2)
{
	echo "limit over";
}

Done!!..!!  🙂

Multiple Image Uploader

Hello guys,

Today i need to develop Multiple Image Uploader for my project purpose, so first i searched on Google and i also got so many widgets, but all widget have heavy coding which is not easy to understand. so i thought let’s develop it with my own logic and i did it.!

multiimageuploader

I used JQuery and HTML and PHP for this small widget. don’t worry this is very simple and easy to understand. 🙂

so let’s Go For CodinG…..!

HTML-CODE

<html>
<head>
<title>Image Uploader</title>
<script src="jquery.js"> </script>
<script>
var i=0;
$("#add").live("click",function(){
i++;
document.getElementById("count").value=i;
$("#file").append("<input type='file' name='file"+i+"'><br>");
});
</script>
</head>
<body>
<div style="border:2px solid blue; width:550px;">
<form method="post" action="uploadarticle.php" enctype="multipart/form-data">
<input type="hidden" name="count" id="count">
<table border="0">
<tr>
<td> <b>Upload Image here </b></td>
</tr>
<tr>
<tr>
<td> <input type="file" name="file0"> </td><td> <img src="zoomin1.png" width="35" heigth="35" id="add"> </td>
<td> &nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" value="upload"> </td>
</tr>
</table>
<div id="file"></div>
</form>
</div>
</body>
</html>

PHP-CODE

<?php
$count = $_POST["count"];
for($i=0;$i<=$count;$i++)
{
    if ($_FILES["file".$i]["error"] > 0)
    {
         echo "Error: " . $_FILES["file".$i]["error"] . "<br />";
    }
   else
   {
      move_uploaded_file($_FILES["file".$i]["tmp_name"],"upload/" .      $_FILES["file".$i]["name"] );
     echo "<script>alert('IMAGE uploaded successfully')</script>";
   }
}
?>

NOTE:

  • Create directory with name upload to run this widget.
  • and you must have jquery.js file inside your current directory.
  • put bellow image in your current directory. or you can also place your image if u don’t like mine . :/

zoomin1

 

Hope you like my widget.!!
🙂

Image upload validation using php

hello guys,

sometime it is necessary to check whether the uploaded file is valid Image file or not ?  or sometime you need to allow user to upload only few image extension file.  let’s say you want to allow user to upload only (gif, jpeg, jpg, png) extension file.

so let’s go for coding.

HTML Coding

<body>
<form method="post" action="uploadimage.php" enctype="multipart/form-data">
<table>
<tr>
<td> Upload Image here : </td>
<td> <input type="file" name="file"> </td>
</tr>
<tr>
<td> <input type="submit" value="upload"> </td>
</tr>
</table>
</form>
</body>

PHP Coding (uploadimage.php)

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$arr = explode(".", $_FILES["file"]["name"]);
$extension = end($arr);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jpg"))
&& in_array($extension, $allowedExts))
{
   if ($_FILES["file"]["error"] > 0)
    {
        echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }
    else
    {
      move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"] );
      echo "<script>alert('file uploaded successfully')</script>";
     }
 }
else
{
      echo "Invalid file";
}
?>

Please make sure that you have upload folder in your current directory.!

Hope you enjoy this small script!
🙂