Home Function Disattiva la variabile di sessione

Disattiva la variabile di sessione

250
0

Ogni volta che i dati vengono archiviati utilizzando i cookie, esiste la possibilità che un hacker inserisca alcuni dati dannosi nel computer dell’utente per danneggiare qualsiasi applicazione. Quindi è sempre consigliabile utilizzare sessioni PHP per archiviare informazioni sul server piuttosto che su un computer. Ogni volta che sono necessari dati su molte pagine di un sito Web o di un’applicazione, viene utilizzata PHP Session .

Questa sessione crea un file temporaneo in una cartella che memorizza le variabili registrate con i loro valori che vengono resi disponibili per l’intero sito web. Questa sessione termina quando l’utente si disconnette dal sito o dal browser. A ogni utente diverso vengono assegnati ID di sessione univoci collegati ai post o alle e-mail di un individuo.

<?php
  session_start();
  echo session_id();
?>

Nota: gli ID di sessione vengono generati casualmente dal motore PHP ed è difficile da distinguere.

Esempio: la seguente funzione PHP annulla la registrazione o cancella una variabile di sessione ogni volta che $_SESSION viene utilizzato in qualche codice. Viene utilizzato principalmente per distruggere una singola variabile di sessione.

unset($_SESSION['nome_variabile']);
<!DOCTYPE html> 
<html> 

<head> 
	<style> 
		body { 
			width: 450px; 
			height: 300px; 
			margin: 10px; 
			float: left; 
		} 
		
		.height { 
			height: 10px; 
		} 
	</style> 
</head> 

<body> 
	<h1 style="color:green">GeeksforGeeks</h1> 
	<b> PHP Unset session variables </b> 
	<div class="height"> </div> 
<?php
	
// start a new session 
session_start(); 
		
// Check if the session name exists 
if( isset($_SESSION['name']) ) { 
	echo 'Session name is set.'.'<br>'; 
} 
else { 
	echo 'Please set the session name !'.'<br>'; 
} 
echo'<br>'; 
$_SESSION['name'] = 'John'; 

//unset($_SESSION['name']);	 
echo "Session Name : ".$_SESSION['name'].'<br>'; 
	
?> 
</body> 
</html> 
<!DOCTYPE html> 
<html> 

<head> 
	<style> 
		body { 
			width: 450px; 
			height: 300px; 
			margin: 10px; 
			float: left; 
		} 
		
		.height { 
			height: 10px; 
		} 
	</style> 
</head> 

<body> 
	<h1 style="color:green">GeeksforGeeks</h1> 
	<b> PHP Unset session variables </b> 
	<div class="height"> </div> 
<?php
	
// start a new session 
session_start(); 
		
// Check if the session name exists 
if( isset($_SESSION['name']) ) { 
	echo 'Session name is set.'.'<br>'; 
} 
else { 
	echo 'Please set the session name !'.'<br>'; 
} 
echo'<br>'; 
$_SESSION['name'] = 'John'; 

//unset($_SESSION['name']);	 
echo "Session Name : ".$_SESSION['name'].'<br>'; 
	
?> 
</body> 
</html> 

Output: quando commentiamo unset($_SESSION[‘name’]) , ottieni il seguente output.
Nota: la funzione PHP session_start() è sempre scritta all’inizio di qualsiasi codice.

Quando eseguiamo unset($_SESSION[‘name’]) , rimuovendo il commento dalla riga richiesta nel programma di esempio, ottieni il seguente output.

  unset($_SESSION['name']);      
  //echo "Session Name : ".$_SESSION['name'];  

Produzione:
e vuoi distruggere tutte le variabili di sessione, usa la seguente funzione PHP.

session_destroy();

Se vuoi liberare o liberare lo spazio occupato dalle variabili di sessione per altri usi, viene utilizzata la seguente funzione PHP.

session_unset();

Programma 2:

<!DOCTYPE html> 
<html> 

<head> 
	<title>Unset Session Variable </title> 
</head> 

<body> 
	<h1 style="color:green">GeeksforGeeks</h1> 
	<b> Unset previous session of user</b> 
	<?php 
	echo '<br>'; 
	echo '<br>'; 
	if(isset($_SESSION["user_name"])) 
	{ echo "Welcome "; echo $_SESSION["user_name"]; } 
	?> 
		<form> 
			Input your name here: 
			<input type="text" id="user_id" name="user_id"> 
			<input type=submit value=Submit> 
		</form> 
		<form action="#"> 
			<input type="submit" name="submit"
				value="Unset"
				onclick="UnsetPreviousSession()"> 
		</form> 
	<?php 

	session_start(); 

	if(!isset($_SESSION["user_name"]) && (!empty($_GET['user_id']))) 
	{ 
		$_SESSION["user_name"] = $_GET["user_id"]; 
	} 
	else
	{ 
		UnsetPreviousSession(); 
	} 
	function UnsetPreviousSession() 
	{ 
	unset($_SESSION['user_name']); 
	} 
	?> 
</body> 

</html> 

Previous articlejwt authentication wordpress
Next articlePHP mail() Function

LEAVE A REPLY

Please enter your comment!
Please enter your name here