Wednesday, February 3, 2016

PHP for beginners 2

PHP Sessions example:
Tutorial5_session.php
<?php
session_start();
$_SESSION['Name']='mark';
$_SESSION['Age']=15;
$_SESSION['Weight']=50;
echo "Done";
?>
Tutorial5.php
<?php
session_start();
$name=$_SESSION['Name'];
$age=$_SESSION['Age'];
$weight=$_SESSION['Weight'];
echo $name.'<br>';
echo $age.'<br>';
echo $weight.'<br>';
?>
Unset session:
<?php
session_start();
// session_destroy();
unset($_SESSION['Name']);
?>
PHP Cookie example:
Tutorial6_cookie.php
<?php
// COOKIE EXAMPLE
//          setcookie(name, value, expire, path, domain);
$time = time();
setcookie('student', 'Mark', $time+10);
echo 'Cookie is created '.$time;
?>
Tutorial6.php
<?php
$student =$_COOKIE['student'];
// 10 second after cookie is destroyed
echo $student;
?>
Getting Data from MySQL Database using PHP
Tutorial7.php
<?php
$mysql_host='localhost';
$mysql_user='root';
$mysql_password='root';

if(!@mysql_connect($mysql_host, $mysql_user, $mysql_password)){
             die('Cannot Connect to database');
 } else {
                         if(@mysql_select_db('student_phpexample')){
                        //                      echo 'Connection success';
             } else
                         die('Cannot Connect to database');
 }

?>
Tutorial7_getdata.php
<?php
include 'tutorial7.php';
$query="SELECT * FROM `user_info`";
if($is_query_run=mysql_query($query)){
            // echo "query executed";
            while($query_execute=mysql_fetch_assoc($is_query_run)){
                        echo '<table border ="1" style="width:300px"><tr>';
                        echo '<td>'.$query_execute['Name'].'</td>';
                        echo '<td>'.$query_execute['Surname'].'</td>';
                        echo '<td>'.$query_execute['Age'].'</td>';
                        echo '</tr>';
            }
} else {
            echo "query not executed";
}
?>
PHP Md5 Encryption example:
<?php
$password="pass";
echo md5($password);
if(isset($_POST['password']) && !empty($_POST['password'])){
            $check_password = $_POST['password'];
            if(md5($check_password)==md5($password))
            {
                        echo "Password correct";
            } else {
                        echo "Password is not correct";
            }
}
?>
<form action="tutorial8.php" method="post">
<input type ="text" name="password">
<br>
<input type="submit">

</form>

No comments:

Post a Comment