php session n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
PHP Session PowerPoint Presentation
Download Presentation
PHP Session

Loading in 2 Seconds...

play fullscreen
1 / 14

PHP Session - PowerPoint PPT Presentation


  • 301 Views
  • Uploaded on

PHP Session. ISYS 475. Session. The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session. Session support in PHP consists of a way to preserve certain data across subsequent accesses in the $_SESSION superglobal array.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'PHP Session' - teva


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
php session

PHP Session

ISYS 475

session
Session
  • The web server starts a session when a visitor visiting your web site and assigns a unique id, the session id for the session.
  • Session support in PHP consists of a way to preserve certain data across subsequent accesses in the $_SESSION superglobal array.
  • When a visitor accesses your site, PHP will check automatically or on your request (explicitly through session_start() ) whether a specific session id has been sent with the request. If this is the case, the prior saved data in $_SESSION is recreated.
session start
session_start()
  • Start new or resume existing session
example tracking the number of times a button is clicked
Example: Tracking the number of times a button is clicked

<?php

if (isset($counter)) $counter=$counter+1;

else $counter=0;

?>

<form action="callSelf.php">

<?php

if ($counter>0) echo "You click me $counter times!<br><br>";

else $counter=0;

?>

<input type="submit" value="Click Me" name="btnClick" />

</form>

Note: This program does not work because the value of $counter is lost.

correction save counter in session
Correction: Save $counter in $_SESSION

<?php

session_start();

if (isset($_SESSION["counter"]))

{

$counter=$_SESSION["counter"]+1;

$_SESSION["counter"]=$counter;

}

else

{

$counter=0;

$_SESSION["counter"]=$counter;

}

?>

<form action="callSelf.php">

<?php

if ($counter>0) echo "You click me $counter times!<br><br>";

?>

<input type="submit" value="Click Me" name="btnClick" />

</form>

example save a variable in session and access it from other pages
Example: Save a variable in $_SESSION and access it from other pages
  • In Page 1, add variable $user in $_SESSION
  • Page 2 and Page 3 read variable $user and show a message.
page 1
Page 1

<?php

session_start();

if (isset($_SESSION["user"]))

$user=$_SESSION["user"];

else

{

$user="David";

$_SESSION["user"]=$user;

}

echo " Hello, $user, you are visiting page 1!<br>";

?>

<br>

<a href="page2.php">Visit page 2 click here</a><br>

<a href="page3.php">Visit page 3 click here</a><br>

page 2 3
Page 2, 3

<?php

session_start();

if (isset($_SESSION["user"]))

$user=$_SESSION["user"];

echo "Hello, $user, you are visiting page 2!<br>";

?>

<br>

<a href="page1.php">Visit page 1 click here</a><br>

<a href="page3.php">Visit page 3 click here</a><br>

<?php

session_start();

if (isset($_SESSION["user"]))

$user=$_SESSION["user"];

echo "Hello, $user, you are visiting page 3!<br>";

?>

<br>

<a href="page1.php">Visit page 1 click here</a><br>

<a href="page2.php">Visit page 2 click here</a><br>

page 11
Page 1

<?php

session_start();

if (isset($_SESSION["user"]))

$user=$_SESSION["user"];

else

{

$user="David";

$_SESSION["user"]=$user;

}

if (isset($_SESSION["visited"]))

{

$visited=$_SESSION["visited"];

$visited[count($visited)]="page1";

}

else $visited[0]="page1";

$_SESSION["visited"]=$visited;

echo " Hello, $user, you are visiting page 1!<br>";

var_dump($visited);

?>

<br>

<a href="page2.php">Visit page 2 click here</a><br>

<a href="page3.php">Visit page 3 click here</a><br>

page 2
Page 2

<?php

session_start();

if (isset($_SESSION["user"]))

$user=$_SESSION["user"];

if (isset($_SESSION["visited"]))

{

$visited=$_SESSION["visited"];

$visited[count($visited)]="page2";

}

else

$visited[0]="page2";

$_SESSION["visited"]=$visited;

echo "Hello, $user, you are visiting page 2!<br>";

var_dump($visited);

?>

<br>

<a href="page1.php">Visit page 1 click here</a><br>

<a href="page3.php">Visit page 3 click here</a><br>

page 3
Page 3

<?php

session_start();

if (isset($_SESSION["user"]))

$user=$_SESSION["user"];

if (isset($_SESSION["visited"]))

{

$visited=$_SESSION["visited"];

$visited[count($visited)]="page3";

}

else

$visited[0]="page3";

$_SESSION["visited"]=$visited;

echo "Hello, $user, you are visiting page 3!<br>";

var_dump($visited);

?>

<br>

<a href="page1.php">Visit page 1 click here</a><br>

<a href="page2.php">Visit page 2 click here</a><br>

session id
Session_id()
  • session_id() is used to get or set the session id for the current session.

<?php

session_start();

echo " session id is:" . session_id();

?>

to stop a session session destroy
To Stop a Session: session_destroy()
  • session_destroy() destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session, or unset the session cookie. To use the session variables again, session_start() has to be called.

<?php

session_start();

session_destroy();

header("location:login.php");

?>