Tuesday, 11 March 2014

PHP addslashes()

Definition and Usage

The addslashes() function returns a string with backslashes in front of predefined characters.
The predefined characters are:
  • single quote (')
  • double quote (")
  • backslash (\)
  • NULL
Tip: This function can be used to prepare a string for storage in a database and database queries.
Note: PHP runs addslashes() on all GET, POST, and COOKIE data by default. Therefore you should not use addslashes() on strings that have already been escaped, this will cause double escaping. The function get_magic_quotes_gpc() can be used to check this.

TEST CASE:-

<?php 
$str = addslashes('What does "yolo" mean?');
echo($str); 
?>

 Returns:--   What does \"yolo\" mean?

<?php
$str = "Who's Peter Griffin?";
echo $str . " This is not safe in a database query.<br>";
echo addslashes($str) . " This is safe in a database query.";
?>

Returns --- Who's Peter Griffin? This is not safe in a database query.
                        Who\'s Peter Griffin? This is safe in a database query.




Simple Live Preview Application on keyup in Jquery

form.html

<!doctype>
<html>
  <head>
     <title>Live Preview In Jquery</title>
<style>
body{
  font-family:tahoma,arial;
  font-size:16px;
}
  .total{
    width:800px;
margin:20px auto;
border:1px solid green;
  }
  .left{
    margin-left:5px;
padding-left:5px;
    float:left;
width:370px;
border:1px solid red;
  }
   .right{
margin-left:5px;
padding-left:5px;
    float:left;
width:370px;
border:1px solid red;
background:#c1c1c1;
  }
  .hidden{
   display:none;
   }
.red{
color:red;
}
.footer{
 width:100%;
 float:left;
 background-color:#c1c1c1;
}
.header{
 width:100%;
 float:left;
 background-color:#c1c1c1;
 color:#fff;
 font-size:20px;
 padding:5px;
}
</style>
  </head>
  <body>
<center><p class='header'>Simple Live Preview System In Jquery On KeyupEvent</center>

    <div class='left'>
<h3>Fill the form</h3>
 <form method='post' autocomplete='off' id='frm'>
 <table>
    <tr><td>Name:</td><td><input type='text' name='name' id='name' required maxlength='20'></td></tr>
<tr><td>Email:</td><td><input type='email' name='email' id='email' required></td></tr>
<tr><td>Mobile:</td><td><input type='text' name='mobile' id='mobile' required maxlength='10'></td></tr>
<tr><td>City:</td><td><input type='text' name='city' id='city' required maxlength='30'></td></tr>
<tr><td>State:</td><td><input type='text' name='state' id='state' required maxlength='20'></td></tr>
<tr><td>Pincode:</td><td><input type='text' name='pincode' id='pincode' required maxlength='6'></td></tr>
<tr><td>Area:</td><td><input type='text' name='area' id='area' required></td></tr>
<tr><td><input type='submit' name='Submit' value='Add Details'></td></tr>
</table>
 </form>
</div>
<div class='right'>
 <div class='prev'>
  <h3>Live Preview</h3>
  <p class='area_name hidden'>Name:- <span id='name_preview'></span></p>
  <p class='area_email hidden'>Email:- <span id='email_preview'></span></p>
  <p class='area_mobile hidden'>Mobile:- <span id='mobile_preview'></span></p>
  <p class='area_city hidden'>City:- <span id='city_preview'></span></p>
  <p class='area_state hidden'>State:- <span id='state_preview'></span></p>
  <p class='area_pincode hidden'>Pincode:- <span id='pincode_preview'></span></p>
  <p class='area_area hidden'>Area:- <span id='area_preview'></span></p>
 </div>
<div class='confirmation hidden'>
  <p>Form Submiited Successfully</p>
</div>
</div>

<script src='http://code.jquery.com/jquery-1.11.0.min.js'></script>
<script src='preview.js'></script>
<center><p class='footer'>&copy; - 2014.&nbsp; <font color='red'>Dhrubojyoti Das</font> production</p></center>
  </body>

</html>

And this is our jquery file
preview.js

$(document).ready(function(){
  //alert("Jquery Loaded");
  $('#name').keyup(function(){
    var name = $(this).val();
if(name != "")
{
$('.area_name').show();
     $('#name_preview').html(name);
}
else{
  $('.area_name').hide();
}
  });
  
  $('#email').keyup(function(){
    var email = $(this).val();
if(email != "")
{
$('.area_email').show();
     $('#email_preview').html(email);
}
else{
  $('.area_email').hide();
}
  });
  
  $('#mobile').keyup(function(){
    var mobile = $(this).val();
if(mobile != "")
{
$('.area_mobile').show();
     $('#mobile_preview').html(mobile);
}
else{
  $('.area_mobile').hide();
}
  });
  
  $('#city').keyup(function(){
    var city = $(this).val();
if(city != "")
{
$('.area_city').show();
     $('#city_preview').html(city);
}
else{
  $('.area_city').hide();
}
  });
  
  $('#state').keyup(function(){
    var state = $(this).val();
if(state != "")
{
$('.area_state').show();
     $('#state_preview').html(state);
}
else{
  $('.area_state').hide();
}
  });
  
  $('#pincode').keyup(function(){
    var pincode = $(this).val();
if(pincode != "")
{
$('.area_pincode').show();
     $('#pincode_preview').html(pincode);
}
else{
  $('.area_pincode').hide();
}
  });
  
  $('#area').keyup(function(){
    var area = $(this).val();
if(area != "")
{
$('.area_area').show();
     $('#area_preview').html(area);
}
else{
  $('.area_area').hide();
}
  });
  
  $('#frm').submit(function(){
     alert("Form submitted successfully");
 
  });
  


});

Live Demo

WORD COUNT USING JAVASCRIPT FUNCTION

WORD COUNT USING JAVASCRIPT FUNCTION

<script>
function checkwords(cur){
var maxwords=10
if (document.images){
var temp=cur.words.value.split(" ")
if (temp.length>maxwords){
alert("Please restrain your input to 10 or less words!");
document.getElementById('words').focus();
return false;
}
}
}
</script>

<form onsubmit="return checkwords(this)">
<strong>In 10 words or less, please tell us your qualifications:</strong><br><textarea rows="5" cols="38" name="words" id='words'></textarea>
<br><input type="submit" value="Submit!">
</form>

Monday, 10 March 2014

Custom Alert Box Message Using Javascript

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style type="text/css">
#dialogoverlay{
display: none;
opacity: .8;
position: fixed;
top: 0px;
left: 0px;
background: #c1c1c1;
width: 100%;
z-index: 10;
}
#dialogbox{
display: none;
position: fixed;
background: red;
border-radius:7px;
width:550px;
z-index: 10;
}
#dialogbox > div{ background:#FFF; margin:8px; }
#dialogbox > div > #dialogboxhead{ background: #666; font-size:19px; padding:10px; color:#CCC; }
#dialogbox > div > #dialogboxbody{ background:#333; padding:20px; color:#FFF;fon-family:verdana; }
#dialogbox > div > #dialogboxfoot{ background: #666; padding:10px; text-align:right; }
.btn{
  background:yellow;
  padding:10px;
  border:2px solid green;
  width:200px;
}
</style>
<script>
function CustomAlert(){
this.render = function(dialog){
var winW = window.innerWidth;
   var winH = window.innerHeight;
var dialogoverlay = document.getElementById('dialogoverlay');
   var dialogbox = document.getElementById('dialogbox');
dialogoverlay.style.display = "block";
   dialogoverlay.style.height = winH+"px";
dialogbox.style.left = (winW/2) - (550 * .5)+"px";
   dialogbox.style.top = "100px";
   dialogbox.style.display = "block";
document.getElementById('dialogboxhead').innerHTML = "Alert Box Header";
   document.getElementById('dialogboxbody').innerHTML = dialog;
document.getElementById('dialogboxfoot').innerHTML = '<button onclick="Alert.ok()" class="btn">OK</button>';
}
this.ok = function(){
document.getElementById('dialogbox').style.display = "none";
document.getElementById('dialogoverlay').style.display = "none";
}
}
var Alert = new CustomAlert();
</script>
</head>
<body>
<div id="dialogoverlay"></div>
<div id="dialogbox">
  <div>
    <div id="dialogboxhead"></div>
    <div id="dialogboxbody"></div>
    <div id="dialogboxfoot"></div>
  </div>
</div>
<h1>Custom alert box message</h1>

<button onclick="alert('This is regular and default alert message.')">Default Alert</button>

<button onclick="Alert.render('Hello frnds this is a custom alertbox message for you all, from:- php4allu')">Custom Alert</button>

</body>
</html>

Live Demo

Javascript toggle password security field

A simple application in javascript to hide/show password field value in the password box


<script type="text/javascript" language="javascript">
function togglePassword() {
var upass = document.getElementById('upass');

var toggle_text = document.getElementById('test_toggle');
if(upass.type == "password"){
upass.type = "text";
toggle_text.innerHTML = "Hide Password Characters";
} else {
upass.type = "password";
toggle_text.innerHTML = "Show Password Characters";
}
}
</script>
Password: <input id="upass" type="password" name="userpass">
<input id="toggleBtn" type="checkbox" onclick="togglePassword()"><span id='test_toggle'>Show Password Character</span>

Live Demo

Friday, 7 March 2014

Secure image upload in php with random salt

This is a short tutorial for image upload with random salt (haven't included any other checking like file type/size--- but you can include it)

<?php
  //////////////////////////////////////////////////////////////////
  //////////// script created by DHRUBOJYOTI DAS ///////////////////
  /////////////////////////////////////////////////////////////////
  if(isset($_POST['Submit']))
  {
  ///// picking the image and storing in variable $image1 and creating a temporary variable $tmp_image1
  $image1=$_FILES['image1']['name'];
  $tmp_image1=$_FILES['image1']['tmp_name'];
 
  ////////////////////////////////////////////////////////////////
  ////////// Now the allowed extension type validation is done ///
  //////////by javascript function Checkfiles1() [check below]
  ///////// better practice do it again b php ///////////////////
  ///////// You can also validate the size of the image////////
  ////////////////////////////////////////////////////////////////
 
 
         ///// now creating my own salt (password fro each uploaded image)
         $password = "ABCDXYZW1234567890EFGLWKMNOPQRST";
    $string = str_shuffle($password);
         $salt=substr($string,0,8);

if(!empty($image1))
{
  $image1_name=$salt."_".$image1;
 
  //// uploading the image using move_uploaded_file function
//// replace your uploaded image folder name by images
$uploading = move_uploaded_file($tmp_image1, "images/".$image1_name);
if($uploading)
{
  echo "<font color='green'>File uploaded, now check your uploaded folder for the image throght the below link";
  echo "<br/>New file name :- ". $image1_name;
  ////// NOTE:- when inserting into database insert ($image1_name) not ($image1)
  /////// then insert it into your databse or something else
  echo "<br/>Thank You from Dhrubojyoti Das</font>";
}
else{
   echo "Something went wrong";
}
}
else{
  echo "Upload image to check the demo";
}


   }
?>
<html>
  <head>
    <title>
 Secure File Upload Script in PHP
</title>
<script>
function Checkfiles1()
{
var fup = document.getElementById('image1');
var fileName = fup.value;
var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "PNG" || ext == "png")
{

return true;
}
else
{
alert("Upload Gif or Jpg or png images only");
fup.value="";
fup.focus();
return false;
}
}
function validate(){
  var mar=true;
  var image = document.frm.image1.value;
  if(image == "")
  {
     mar = false;
alert("Select your image");
document.frm.image1.focus();
  }
 
    return mar;
 
}

</script>
  </head>
  <body>
  <h2>Secure image upload with random salt</h2>
  <form name='frm' method='post' enctype="multipart/form-data" onsubmit='return validate();'>
    Upload Image&raquo; <input type='file' name='image1' id='image1' onchange='Checkfiles1();'><br/>
    <br/r><input type='submit' name='Submit' value='Upload'>
  </form>
  <a href='images/'>Check Uploaded Image</a>
  </body>
</html>

Create  the images folder

Live Demo

Monday, 3 March 2014

Simple application to swap css sheets uisng button

<html>
    <head>
    <title>
   Css Swap
</title>
<link rel='stylesheet' id='css' type='text/css' href='default.css'>
<script>
 function swapcss(sheet)
 {
  document.getElementById('css').setAttribute('href',sheet);
 }
</script>
</head>
<body>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    <input type='button' value='Blue CSS' onclick='swapcss("blue.css");'>
<input type='button' value='Red CSS' onclick='swapcss("red.css");'>
<input type='button' value='Default CSS' onclick='swapcss("default.css");'>
</body>
</html>

Live Demo