Tuesday, 9 June 2020

setting php sesssion

setting php sesssion
zoom.php
<?php
$title = '<h1> telugu computer world </h1>';
$title2 = '<h1> php </h1>';
?>
page1
<?php
include 'zoom.php';
after adds this include like bootstrap, we can access that template data
echo $title2;
print that title2
?>
how to store the information in user browser
set.php
<?php
session_start();
$_SESSION['name'] = 'rafique';
variable name (key) - load that value
?>
aftere this page open, if u open vies.php
whenever this page opens, this cookies store in user browser
view .pjp
<?php
session_start();
after sesion start only that cookei will execute
echo = $_SESSION['name'];
if that set.php page opens, then if u open this page view page that prints the name called
?>
rafique
session1.php
<?php  
session_start();  
?>  
<html>  
<body>  
<?php  
$_SESSION["user"] = "Sachin";  
echo "Session information are set successfully.<br/>";  
?>  
<a href="session2.php">Visit next page</a>  
</body>  
</html>  

session2.php

<?php  
session_start();  
?>  
<html>  
<body>  
<?php  
echo "User is: ".$_SESSION["user"];  
?>  
</body>  
</html>  











how to know how many times customer visited the page
using session_start- started to store the cookies in customer browser, if session started ,means it is set, counter increment,if u see so many times, it will increment and tells, how many times u browse
Live Demo
<?php
   session_start();
  
   if( isset( $_SESSION['counter'] ) ) {
      $_SESSION['counter'] += 1;
   }else {
      $_SESSION['counter'] = 1;
   }
      
   $msg = "You have visited this page ".  $_SESSION['counter'];
   $msg .= "in this session.";
?>

<html>
  
   <head>
      <title>Setting up a PHP session</title>
   </head>
  
   <body>
      <?php  echo ( $msg ); ?>
   </body>
  
</html>


If session is started
Lifetime of cookie -'cookie_lifetime' => 86400,
Storing or loading some data to sessions

$_SESSION['favcolor'] = 'green';
$_SESSION
['animal']   = 'cat';
$_SESSION
['time']     = time();




Shown to some other page
<?php // Starting session session_start(); // Accessing session data echo

'Hi, ' . $_SESSION["firstname"] . ' ' . $_SESSION["lastname"]; ?>
echo $_SESSION['favcolor']; // green
echo 
$_SESSION['animal'];   // cat
echo 
date('Y m d H:i:s'$_SESSION['time']);

Destroys the session
PHP session_destroy() function is used to destroy all session variables completely.
<?php // Starting session session_start(); // Destroying session session_destroy(); ?>

how to set a cookie
cookies are used to store the information of user at browser of client
its helpfult to identify the user where he stops
using setcookie we can set the cookie with the value given and name
and we can set the time how much time cookie willl be theree
1. setcookie("CookieName""CookieValue");/* defining name and value only*/  
2. setcookie("CookieName""CookieValue", time()+1*60*60);//using expiry in 1 hour(1*60*60 seconds or 3600 seconds)  
3. setcookie("CookieName""CookieValue", time()+1*60*60, "/mypath/""mydomain.com", 1);  
how can we access the cookie value using cookie name
$value=$_COOKIE["CookieName"];//returns cookie value  
1. <?php  
2. setcookie("user""Sonoo");  
3. ?>  
4. <html>  
5. <body>  
6. <?php  
7. if(!isset($_COOKIE["user"])) {  
8.     echo "Sorry, cookie is not found!";  
9. } else {  
10.     echo "<br/>Cookie Value: " . $_COOKIE["user"];  
11. }  
12. ?>  
13. </body>  
14. </html>  
if u set the expiration data in past, it wll delete the cookie
1. <?php  
2. setcookie ("CookieName""", time() - 3600);// set the expiration date to one hour ago  
3. ?>  
PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.
$_SESSION["user"] = "Sachin";  
echo $_SESSION["user"];  










how can we set the session, how can we view the sessions
How to print the data which is from template in another page
what is meant by session, or cookies , is security is greater tha n cookies
can we use if the custoemr is opened this page, later some page is opened, can we  print from the page on that page
how to print the data from anotherr page if u opened that set file only?




include
echo
session_start()
$_SESSION['NAME']
session_start
$_SESSION['NAME']

No comments:

Post a Comment