Saturday 24 October 2015

session example in php very easy to understand beginner must read



<?php
session_start();

if(!isset($_SESSION['userID']) || (trim($_SESSION['userID']) == '')){
    header('location:index.php');
    exit();
}

$session_id = $_SESSION['userID'];
$session_id = $_SESSION['username'];


?>
$_SESSION :-   userID and username is a DATABASE NAME  as shown in dig


Session variables are used to store individual client’s information on web server for later use,  as web server does not know which client’s request to be respond because, HTTP address does not maintained state.


>>Cookies and Session diff
  1. Cookies are returned and stored in the user's browser, session data is stored on your web server.
  2. The life span of a cookie can be set to almost any duration of your choosing. PHP sessions have a predetermined short life. The exact life span depends on how your web host has configured PHP on your server.
  3. Depending on how your web server is configured, session data is often stored in a public temporary directory on the server. As such it is possible that other users on the server may be able to peek at the data you store there.

A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit.
The location of the temporary file is determined by a setting in the php.ini file called session.save_path. Bore using any session variable make sure you have setup this path.
When a session is started following things happen −
  • PHP first creates a unique identifier for that particular session which is a random string of 32 hexadecimal numbers such as 3c7foj34c3jj973hjkop2fc937e3443.
  • A cookie called PHPSESSID is automatically sent to the user's computer to store unique session identification string.
  • A file is automatically created on the server in the designated temporary directory and bears the name of the unique identifier prefixed by sess_ ie sess_3c7foj34c3jj973hjkop2fc937e3443.

-----------------------------------------------------------
$_GET EXAMPlE

 <table align="center" border="1" cellspacing="0" width="500">
                    <tr>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Action</th>
                    </tr>
                    <?php
                    $sql = "SELECT * FROM info_tbl";
                    $result = $conn->query($sql);
                    if($result->num_rows > 0){
                    while($row = $result->fetch_array()){
                        ?>
                        <tr>
                            <td align="center"><?php echo $row['firstName'];?></td>
                            <td align="center"><?php echo $row['lastName'];?></td>
                            <td align="center"><a href="edit.php?infoID=<?php echo md5($row['infoID']);?>">Edit
                            </a>/<a href="delete.php?infoID=<?php echo md5($row['infoID']);?>">Delete</a></td>
                        </tr>
                        <?php
                            }  
                        }else{
                            echo "<center><p> No Records</p></center>";
                        }

---------------------------------------------------

<?php
include 'conn.php';
include 'session.php';

$id = $_GET['infoID'];
$view = "SELECT * from info_tbl where md5(infoID) = '$id'";
$result = $conn->query($view);
$row = $result->fetch_assoc();

if(isset($_POST['update'])){

    $ID = $_GET['infoID'];

    $fn = $_POST['fname'];
    $ln = $_POST['lname'];

    $insert = "UPDATE info_tbl set firstName = '$fn', lastName = '$ln' where md5(infoID) = '$ID'";
   
    if($conn->query($insert)== TRUE){

            echo "Sucessfully update data";
            header('location:maintenance.php');           
    }else{

        echo "Ooppss cannot add data" . $conn->error;
        header('location:maintenance.php');
    }
    $conn->close();
}
?>

 ........................................................................................................
FOREACH ARRAY

No comments:

Post a Comment