Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Promo code / Coupon Code Generator

PHP Code for promo/coupon code generator



<?php
$chars = array (0,1,2,3,4,5,6,7,8,9,'A', 'B', 'C', 'D', 'E', 'F', 'G','H', 'I', 'J', 'K', 'L', 'M', 'N', '0', 'P','Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
$code = '';
$letter="10";
for ($i = 0; $i < $letter; $i++) { $code .= $chars[rand(0, count($chars) - 1)];}
echo $code;
?>

Rename To Upload File in PHP

when a upload files to automatically  rename files using time and file name  to encrypt the file and store to database

Delete Multiple rows

Delete Multiple rows in php first count the values sy : count(); count to select values in table example 123456 rows .next for loop using to divided a values to delete a table rows.


Http to Https direct using PHP

Http to Https direct  using PHP with  www Extension 

<?php
$protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
if ($_SERVER["SERVER_PORT"] != "80")
{
$pageURL= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
}
else
{
$pageURL= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

if($protocol!='https' || strpos($pageURL,'www') === false)
{
$protocol='https';

if (strpos($pageURL,'www') === false)
{
$www='www.' ;
}
$url=$protocol.'://'.$www.$pageURL;
header("Location: ".$url."");
exit();
}

PHP coding for download database to CSV format

This is download database  in  CSV format.why we are  use this ? if  database can  be corrupted . you can use the  download and  import easy to database.


Email Vaildation in PHP

This is simple email validation in PHP ,JavaScript  have easy to hack to  validation script and much more
.In php scripts is very secure  and safe. no one can hack this validation ,generally a PHP coding is very  secure .it accepted a valid  email only

!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$

this code is only  accepted only valid  email address


Calculate average inserted fields in a database table row

This is Calculate average  inserted  fields in a database table  row.this is simple coding to calculate a  percentage  %.Read this full coding to this very simple to calculate.This css also  very simple to create.


 

Change Password in PHP

hi.this is change password  using PHP.change password    u 1st select a  table  and check a password  is correct and if correct a   update a  password .else display a error message.hey this  just update a  password if old is  password is match to update a  password....,


Simple PHP code for Calculating Age

        hi ..This is a  Calculating age using PHP.select your date of birth and get your age.it very simple coding to calculate age. mktime()  using to Calculating age ..



Ascending and Descending Order in PHP

Hi.this is order by ascending and descending order  to alphabets and number .this   very a simple  to PHP MySQL  .if order by ascending ("ORDER BY FILED_NAME ASC")  or order by descending  ("ORDER BY FILED_NAME DESC") ..,

ORDER BY Ascending


<?php
$select= mysql_query("SELECT * FROM table_name ORDER BY filed_name ASC"); ?>

Implode and Explode in PHP

Implode - Join array elements with a string
Explode - Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter.  

Captcha in PHP

CAPTCHA  is stand for "Completely Automated Public Turing Test To Tell Computers and Humans Apart."

A CAPTCHA is a program that can generate and grade tests that humans can pass but current computer programs cannot.

CAPTCHA  is used for 
1.Preventing Comment Spam in Blogs.

2.Protecting Website RegistrationProtecting Website Registration.

3.Online Polls. 


4.Preventing Dictionary Attacks. 


5.Search Engine Bots.  

Output:

 

Source code:

<?php

$CAPTCHA_STRENGTH=5;

$random_str = md5(microtime());

$captcha_str = substr($random_str, 0, $CAPTCHA_STRENGTH);

$width = ($CAPTCHA_STRENGTH * 10)+10;

$height = 20;

$captcha_img =ImageCreate($width, $height);

$back_color = ImageColorAllocate($captcha_img, 0, 0, 0);

$text_color = ImageColorAllocate($captcha_img, 255, 255, 255);

$line_color = ImageColorAllocate($captcha_img, 255, 0, 255);

ImageFill($captcha_img, 0, 0, $back_color);

for($i= 0;$i< $width; $i += 5)
    ImageLine($captcha_img, $i, 0, $i, 20, $line_color);

for($i = 0; $i< 20; $i += 5)
    ImageLine($captcha_img, 0, $i, $width, $i , $line_color);

ImageString($captcha_img, 5, 5, 2, $captcha_str, $text_color);

header("Content-type: image/jpeg");

ImageJPEG($captcha_img);

ImageDestroy($captcha_img);
?>

PHP Coding for Voting

Voting in PHP


This is code for put  up voting and down voting to any content .

It's Contain Simple PHP coding.


U need run this program  config.php,up_vote.php,down_vote.php. so download   below  coding.

voting.php 

<html>
<head>
<title>Voting with PHP</title>
<style type="text/css">
body
{
font-family:'Georgia', Times New Roman, Times, serif;
}
#main
{
height:80px; border:1px solid #09F;margin-bottom:7px;
width:500px;
}
a
{
color:#DF3D82;
text-decoration:none;

}
a:hover
{
color:#DF3D82;
text-decoration:underline;

}
.up
{
height:40px; font-size:24px; text-align:center; background-color:#009900; margin-bottom:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}
.up a
{
color:#FFFFFF;
text-decoration:none;

}
.up a:hover
{
color:#FFFFFF;
text-decoration:none;

}
.down
{
height:40px; font-size:24px; text-align:center; background-color:#cc0000; margin-top:2px;
-moz-border-radius: 6px;-webkit-border-radius: 6px;
}

.down a
{
color:#FFFFFF;
text-decoration:none;

}
.down a:hover
{
color:#FFFFFF;
text-decoration:none;

}
.box1
{
float:left; height:80px; width:50px;
}

.box2
{
float:left; width:440px; text-align:left;
margin-left:10px;height:60px;margin-top:10px;
font-weight:bold;

font-size:18px;
}
img
{
border:none;
padding-top:7px;

}
</style>
</head>

<body>
<div align="center">
<h3>Voting With PHP</h3>
<?php
include('config.php');
$sql=mysql_query("SELECT * FROM messages  LIMIT 9");
while($row=mysql_fetch_array($sql))
{
$msg=$row['msg'];
$mes_id=$row['mes_id'];
$up=$row['up'];
$down=$row['down'];
?>

<div id="main">
<div class="box1">
<div class='up'>
<a href="up_vote.php?id=<?php echo $mes_id; ?>" class="vote" id="<?php echo $mes_id; ?>" name="up">
<?php echo $up; ?></a></div>

<div class='down'>
<a href="down_vote.php?id=<?php echo $mes_id; ?>" class="vote" id="<?php echo $mes_id; ?>" name="down">
<?php echo $down; ?></a></div>
</div>

<div class='box2' ><?php echo $msg; ?></div>

</div>
<?php } ?>
</div>
</body>
</html>



Download:


PHP with voting

PHP Coding for GET your IP Address

This is  get your  IP address .it work on online only it give to your IP address.get IP address different way  in the PHP . In PHP as used $_SERVER['REMOTE_ADDR'] ; and getenv("REMOTE_ADDR") ;  to get IP address easy.

Method 1

<?php
//Gets the IP address using $_SERVER['REMOTE_ADDR'] ;
$ip =$_SERVER['REMOTE_ADDR'] ;
echo "Your IP is " . $ip;
?>

Method 2

<?php

//Gets the IP address using  getenv("REMOTE_ADDR") ;
$ip = getenv("REMOTE_ADDR") ;
echo "Your IP is " . $ip;
?>

PHP Coding for Page view Count

hi.this is a page view count.it count automatically to any one enter the website.it start in 1.to refresh or reload a website it's automatically increment a count  .
 
   1.Register the session " session_register('contador'); ".

   
2.next increment contador " ++$_SESSION['contador']".

Note : u refresh or reload page automatically increment value 


Source Code:


<?php
session_register('contador');
echo '<a href="'.$PHP_SELF.'?'.$SID.'">Contador vale:'.++$_SESSION['contador'].'</a>';
?>


Pagination in PHP


         On the Internet, pagination is used for such things as displaying a limited number of results on search engine results pages, or showing a limited number of posts when viewing a forum thread. Pagination is used in some form in almost every web application to divide returned data and display it on multiple pages. Pagination also includes the logic of preparing and displaying the links to the various pages. Pagination can be handled client-side or server-side. 

         Server-side pagination is appropriate for large data sets providing faster initial page load, accessibility for those not running Javascript, and complex view business logic. Correctly implementing pagination can be difficult.

       Server-side pagination is more common. Client-side pagination can be used when there are very few records to be accessed, in which case all records can be returned, and the client can use JavaScript to view the separate pages. By using AJAX, hybrid server/client-side pagination can be used, in which Javascript is used to request the subsequent page which is loaded and inserted into the Document Object Model via AJAX. 

        There are many different usability questions such as should "previous" and "next" links be included, how many links to pages should be displayed, and should there be a link to the first and last pages. Also ability to define the number of records displayed .
 




Source Code


<html>
<head>
<title>Pagination</title>
<link href="style/pagination.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="876" border="1" align="center" cellspacing="0">
<tr>
<th width="20" scope="col">Emp_id</th>
<th scope="col">Emp Name</th>
<th scope="col">Date of join</th>
<th scope="col">Experince</th>
<th scope="col">Salary</th>
</tr>

<?php
$conn=mysql_connect("localhost","root","");
$sdb=mysql_select_db("pagination",$conn);

$tableName="emp_details"; //page name
$targetpage = "pagination.php";
$limit = 3; //limit of dispaly

$query = "SELECT COUNT(*) as num FROM $tableName";
$total_pages = mysql_fetch_array(mysql_query($query));
$total_pages = $total_pages[num];

$stages = 3;
$page = mysql_escape_string($_GET['page']);
if($page){
$start = ($page - 1) * $limit;
}else{
$start = 0;
}

// Get page data
$sel = mysql_query("select * FROM $tableName LIMIT $start, $limit");
while($row1=mysql_fetch_array($sel))
{
$id=$row1['emp_id'];
$name=$row1['emp_name'];
$doj=$row1['doj'];
$experince=$row1['experince'];
$salary=$row1['salary'];
?>
<tr >
<td height="20"><?php echo $id;?></td>
<td><?php echo $name; ?></td>
<td><?php echo $doj; ?></td>
<td><?php echo $experince ?>year</td>
<td><?php echo $salary ?></td>

</tr>
<?php } ?>
<tr><td colspan="5" >
<?php

// Initial page num setup
if ($page == 0){$page = 1;}
$prev = $page - 1;
$next = $page + 1;
$lastpage = ceil($total_pages/$limit);
$LastPagem1 = $lastpage - 1;


$paginate = '';
if($lastpage > 1)
{

$paginate .= "<div class='paginate'>";
// Previous
if ($page > 1){
$paginate.= "<a href='$targetpage?page=$prev'>previous</a>";
}else{
$paginate.= "<span class='disabled'>previous</span>"; }


// Pages
if ($lastpage < 7 + ($stages * 2)) // Not enough pages to breaking it up
{
for ($counter = 1; $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
elseif($lastpage > 5 + ($stages * 2)) // Enough pages to hide a few?
{
// Beginning only hide later pages
if($page < 1 + ($stages * 2))
{
for ($counter = 1; $counter < 4 + ($stages * 2); $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// Middle hide some front and some back
elseif($lastpage - ($stages * 2) > $page && $page > ($stages * 2))
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $page - $stages; $counter <= $page + $stages; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
$paginate.= "...";
$paginate.= "<a href='$targetpage?page=$LastPagem1'>$LastPagem1</a>";
$paginate.= "<a href='$targetpage?page=$lastpage'>$lastpage</a>";
}
// End only hide early pages
else
{
$paginate.= "<a href='$targetpage?page=1'>1</a>";
$paginate.= "<a href='$targetpage?page=2'>2</a>";
$paginate.= "...";
for ($counter = $lastpage - (2 + ($stages * 2)); $counter <= $lastpage; $counter++)
{
if ($counter == $page){
$paginate.= "<span class='current'>$counter</span>";
}else{
$paginate.= "<a href='$targetpage?page=$counter'>$counter</a>";}
}
}
}

// Next
if ($page < $counter - 1){
$paginate.= "<a href='$targetpage?page=$next'>next</a>";
}else{
$paginate.= "<span class='disabled'>next</span>";
}

$paginate.= "</div>";


}
echo $total_pages.' Results';
// pagination
echo $paginate;
?></td></tr>
</table>

</body>
</html>

PHP coding for finding browser and OS platform

This is find to web browser and OS platform using PHP.   preg_match() function using a identify to  browser and OS  platform .it's is manual coding to display a browser and OS details.simple coding to find browser.

getenv("HTTP_USER_AGENT");

 

<?php
    echo
getenv( "HTTP_USER_AGENT" );
?>

 

getenv() - Gets the value of an environment variable
      HTTP_USER_AGENT - This also is important when using the get_browser() function for finding out  

output:


     Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36

preg_match();

 

<?php
 $viewer = getenv( "HTTP_USER_AGENT" );
 if( preg_match( "/Mozilla/i", "$viewer" ) )
{
$browser = "Mozilla";
}

else
{

echo "undefined"; 
 }
echo $browser;
?>


  preg_match() — Perform a regular expression match
   
   if it's match to display a output else disaply undefined

output:


Mozilla

Source Code:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Simple coding for finding web browser and OS Paltform</title>
</head>
<?php
$viewer = getenv( "HTTP_USER_AGENT" );
$browser = "An unidentified browser";
if( preg_match( "/MSIE/i", "$viewer" ) )
{
$browser = "Internet Explorer";
}
else if( preg_match( "/Chrome/i", "$viewer" ) )
{
$browser = "Chrome";
}
else if( preg_match( "/Mozilla/i", "$viewer" ) )
{
$browser = "Mozilla";
}
$platform = "An unidentified OS!";
if( preg_match( "/Windows/i", "$viewer" ) )
{
$platform = "Windows!";
}
else if ( preg_match( "/Linux/i", "$viewer" ) )
{
$platform = "Linux!";
}
echo("You are using $browser on $platform");
?>
<body>
</body>
</html>

DEMO OUTPUT IS :

    You are using Mozilla on Windows!

Simple PHP coding for dynamic images change by refresh the page

Hi,This is a dynamic images change by refresh the page .this is  randomly change image to refresh  page .simple code to change a  background and images and much more ,, using  rand(); rand(1,5); is  display 1 to 5 images to dynamic change of images,randomly .

Set  random number 

rand(); is used to set the number to change of image or content

$num = rand( 1,2 );

Set a image to randomly change 

Switch() is used to execute a one by one  it execute a  ture part one ...

switch( $num )
{

case 1: $image_file = "1.jpg";
break;
case 2: $image_file = "2.jpg";
break;
}
echo "<img src=
images/".$image_file." />";

Source Code:


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP coding for random image dipaly to refresh apge</title>
</head>
<body>
<?php
srand( microtime() * 1000000 );
$num = rand( 1, 5 );
switch( $num )
{
case 1: $image_file = "1.jpg";
break;
case 2: $image_file = "2.jpg";
break;
case 3: $image_file = "3.jpg";
break;
case 4: $image_file = "4.jpg";
break;
case 5: $image_file = "5.jpg";
break;
}
echo "<img src=
images/".$image_file." />";
?>
</body>
</html>