Monday, February 23, 2015

Encrypt and Decrypt script of a string in PHP

<?php
define("ENCRYPTION_KEY", "!@#$%^&*ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
$string = "This is Death House!";

echo $encrypted = encrypt($string, ENCRYPTION_KEY);
echo "<br />";
echo $decrypted = decrypt($encrypted, ENCRYPTION_KEY);

/**
 * Returns an encrypted & utf8-encoded
 */
function encrypt($pure_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $encrypted_string = mcrypt_encrypt(MCRYPT_BLOWFISH, $encryption_key, utf8_encode($pure_string), MCRYPT_MODE_ECB, $iv);
    return $encrypted_string;
}

/**
 * Returns decrypted original string
 */
function decrypt($encrypted_string, $encryption_key) {
    $iv_size = mcrypt_get_iv_size(MCRYPT_BLOWFISH, MCRYPT_MODE_ECB);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);
    $decrypted_string = mcrypt_decrypt(MCRYPT_BLOWFISH, $encryption_key, $encrypted_string, MCRYPT_MODE_ECB, $iv);
    return $decrypted_string;
}
?>

Wednesday, January 21, 2015

PHP Login and logout script.

We need to create 6 files to complete this task which are:
1.index.html
2.config.php
3.check.php
4.session.php
5.profile.php
6.logout.php

The codes for each file given below:

index.html

This will be tour main page.

<html>
<head>
<title>online chat</title>
<link href="style.css" rel="stylesheet">
</head>
<body>
<div id="main">
    <div id="container">
        <div class="login">
            <form name="login" method="post" action="check.php">
                <input type="text" placeholder="Username" name="username">
                <input type="password" placeholder="Password" name="password">
                <input type="submit" value="Login" name="submit">
            </form>
        </div>
    </div>
</div>
</body>
</html>


Now we create an connection with the database.

config.php

<?php
mysql_connect("localhost","root","");
$a=mysql_select_db("chat");
if(!$a)
{
    echo"error";
}
?>


in above file chat is the name of database i used.

Now create the file where the username and password is checked and redirected to profile page.

check.php

<?php
include('config.php');
session_start();
$error='';
if (isset($_POST['submit'])) {
if (empty($_POST['username']) || empty($_POST['password'])) {
$error = "Username or Password is invalid";
}
else
{

$username=$_POST['username'];
$password=$_POST['password'];
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
echo $username. $password;
$query = mysql_query("select * from user_login where  username='$username' AND pass='$password' ");
$rows = mysql_num_rows($query);
if ($rows == 1) {
$_SESSION['login_user']=$username;
header("location: profile.php");
} else {
$error = "Username or Password is invalid";
}

}
}
?>


The above file check for username and password if the are correct it redirect to profile.php other wiise shows an error.

Before creating an profile .php we create session.php which starts an session.

session.php

<?php
include('config.php');
session_start();

$user_check=$_SESSION['login_user'];

$ses_sql=mysql_query("select username from user_login where username='$user_check'");
$row = mysql_fetch_assoc($ses_sql);
$login_session =$row['username'];
if(!isset($login_session)){

header('Location: index.php');
}
?>


here if session is set and if it is not than it redirects to index.html or index.php where you want to.

then we create profile.php

profile.php

<?php
include('session.php');
include('config.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>Your Home Page</title>
<link href="style.css" rel="stylesheet" type="text/css">
<script>
</script>
</head>
<body>
<div id="profile">
    <div class="head">
    <a href="logout.php">Log Out</a>
    </div>
    <div class="friend">
        <a href="">Amit</a>
    </div>
    <div class="chat">
        <div class="chat_head">
       
        </div>
        <div class="chat_content">
       
        </div>
        <div class="chat_text">
        <textarea placeholder="Type Here" name="chat_text"></textarea>
        </div>
    </div>
</div>
</body>
</html>






And in last we create the logout.php



logout.php





<?php
include('config.php');
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>




The css can be done on your requirements.


Tuesday, November 18, 2014

How to detect the width of a web browser using jQuery?

Detecting the width of a browser window can be very useful when designing a website, it allows you to create a more responsive design which is suited to the current browser dimensions.
Using the small chunk of jQuery below, you can easily detect the current width of a web browser & perform various actions when the width reaches a particular range of values. When redeveloping my site I opted to use this method to add a different body class to the site when the browser width reaches a below particular points: "smaller" when the width reaches below 960px but greater than 400px and "smallest" when the width is below "400px". The additional body classes then allowed me to alter the position/style of particular elements on the page.


(function($){
  //detect the width on page load
  $(document).ready(function(){
    var current_width = $(window).width();
     //do something with the width value here!
    if(current_width < 400){
      jQuery('body').addClass("probably-mobile");
    }
  });

  //update the width value when the browser is resized (useful for devices which switch from portrait to landscape)
  $(window).resize(function(){
    var current_width = $(window).width();
   //do something with the width value here!
    if(current_width < 400){
      jQuery('body').addClass("probably-mobile");
    }
  });

})(jQuery); 

Tuesday, November 11, 2014

How to install Laravel 4 on wamp server?

First thing which you need to check before starting the setup:

Check whether you have the latest version of the php(5.5)

After that you need to download the setup from the link below:

https://getcomposer.org/

Run the setup and install the composer which will download the Laravel 4 files on your server.

After the completion of the setup -> open cmd(Command Promt).

write composer and press enter.

then change the directory to your server directory.

like in my case it was:

c:\user\home> cd c:\wamp\www

this is the comand to be written in cmd.

then your cmd directory would be like this:

c:\wamp\www>

now write the command for installing the Laravel 4.

which is:

composer create-project laravel\laravel yourfoldername --prefer-dist

in cmd it will be as

c:\wamp\www>composer create-project laravel/laravel yourfoldername --prefer-dist

by writing this command an directory with name yourfoldername will be creater in wamp/www.