PHP Sessions
PHP Sessions |
session is a way to store multiple information in variables(without database) to be used across multiple pages
Hi friends in this lesson we are going to learn php sessions..if you are beginners and don't understand much more about php session please read the topic ..
This topic is included a lots of tricks about php session..
=>Create,Read ,Update and Delete in PHP SESSION..
=>session start:
session started with session_start(); function..
=>create.php to creating new session
=>Create,Read ,Update and Delete in PHP SESSION..
PHP Sessions |
session started with session_start(); function..
=>create.php to creating new session
<?php session_start(); //Creating New SESSION Variable called name $_SESSION['name']='ANJAN'; ?>
=> read.php to fetch or read session data
<?php session_start(); //retrive SESSION data //checking SESSION['name'] empty or not if(!empty($_SESSION['name'])){ echo 'Your SESSION Name: '.$_SESSION['name']; }else{ echo "Your SESSION is Empty"; } ?>
=> update.php to update session data
<?php session_start(); //checking SESSION['name'] empty or not if(!empty($_SESSION['name'])){ //Replacing ond name to New one $_SESSION['name']='New Name'; }else{ echo "Your SESSION is Empty"; } ?>
=> delete.php to unset session data
<?php session_start(); //checking SESSION['name'] empty or not if(!empty($_SESSION['name'])){ unset($_SESSION['name']); }else{ echo "Your SESSION is Already Empty"; } ?>
Ok That's it How to set session,read session ,update session and unset session
=>php session page view counter:
count page views using php session |
Now In This lesson we are going to learn how to count page views using php session
create count.php page and copy the below codes..
=>count.php
<?php session_start(); if(!isset($_SESSION['count'])){ $_SESSION['count']='0'; }else{ $_SESSION['count']++; } ?> <h3>You have Refresh This Page: <?php if(!empty($_SESSION['count'])) {echo $_SESSION['count'].' Times'; }else{ echo "You Have not yet Refresh the Page" ; }?></h3> <div><a href="count.php">Refresh</a>
=> Set Website Color in PHP SESSION:
Set and change color in php session |
<h3>Change WebSite color using PHP SESSION</h3> <form method="POST"> <table> <tr> <td>Color:</td> <td><input type="text" name="color" placeholder="Color Name or Code: #ccc" required/></td> </tr> <td></td> <td><input type="submit" name="submit" value="Change Color"/></td> </tr> </table> </form> <?php session_start(); if(isset($_POST['submit'])){ $_SESSION['color']=$_POST['color']; } //define Default Color if SESSION color not set if(empty($_SESSION['color'])){ $_SESSION['color']='#ccc'; }?> <style> table,h3{ background:<?php echo $_SESSION['color']?>; } </style> <table border="1" cellspacing="0"> <tr> <th>Name</th> <th>WebSite</th> </tr> <tr> <td>ANJAN</td> <td>CodeNair.Com</td> </tr> </table>
=> PHP SESSION: Adding Multiple User Data into SESSION:
PHP Sessions |
Hi friends now we are going to learn how to store multiple user information into php session ..this is nice session tricks will help you to understand about basic shopping cart..
index.php
<h3>PHP SESSION :Store Multiple User Info In PHP SESSION</h3> <form method="POST"> <table> <tr> <td>UserName:</td> <td><input type="text" name="name" required/></td> </tr> <tr> <td>Email</td> <td><input type="text" name="email" required/></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Add User"/></td> </tr> </table> </form> <?php session_start(); if(isset($_POST['submit'])){ $user=array( 'name'=>$_POST['name'], //Username form field name 'email'=>$_POST['email'] //email form field name ); $_SESSION['student'][]=$user; } if(isset($_GET['remove'])){ unset($_SESSION['student']); //Redirecting After Unset SESSION header('location:index.php'); } ?> <?php if(!empty($_SESSION['student'])){?> <table class="table" cellspacing="0" border="1"> <tr> <th>Serial</th> <th>Name</th> <th>Email</th> </tr> <?php for($i = 0 ; $i < count($_SESSION['student']) ; $i++) {?> <tr> <td><?php echo $i;?></td> <td><?php echo $_SESSION['student'][$i]['name'];?></td> <td><?php echo $_SESSION['student'][$i]['email'];?></td> </tr> <?php } ?> </table> <a href="index.php?remove=remove">Empty User</a> <?php }else{ echo "You have no User in SESSION"; }?>
=>PHP SESSION: PHP Shopping Cart using SESSION:
PHP Sessions |
if you are beginner in php session this will help you to understand about php add to cart..
index.php
<form method="POST"> <table> <tr> <td>Product ID</td> <td><input type="text" name="id" required/></td> </tr> <tr> <td>Name</td> <td><input type="text" name="name" placeholder="Ex:Pant ,Shirt,Mobile.." required/></td> </tr> <tr> <td>Quantity</td> <td><input type="text" name="qty" placeholder="Ex:2,3,10,50..." required/></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Add to Cart"/></td> </tr> </table> </form> <?php session_start(); if(isset($_POST['submit'])){ $name=$_POST['name']; $id=$_POST['id']; $info=array( "name"=>$name, "id"=>$id, "qty"=>$_POST['qty'] ); $_SESSION['info'][]=$info; //adding New Products } ?> <?php if ( !empty($_SESSION["info"]) ) { $total = 0;?> <table border="1" cellspacing="0"> <tr> <th>ID</th> <th>Name</th> <th>Quantity</th> </tr> <?php foreach ( $_SESSION["info"] as $k=>$v ) { ?> <tr> <td><?php echo $_SESSION['info'][$k]['id']; ?></td> <td><?php echo $_SESSION['info'][$k]['name']; ?></td> <td><?php echo $_SESSION['info'][$k]['qty']; ?></td> </tr> <?php }}?> </table>
that's it friends all about php session ..Keep visiting for more codes Thank You..
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.