Resolving a session within the echo causing the problem

I get the following error and cannot understand why. I've been looking at this for too long.

Mistake:

Parse error: syntax error, unexpected T_UNSET in blah/blah/blah 

My code is:

Essentially, I'm trying to give the user the ability to clear a session by clicking a link. Not sure where I am mistaken in my syntax ... Any help would be greatly appreciated!

NOTE. Yes, my code is in php blocks.

 echo "<span><a href='" . unset($_SESSION['vertical']) . "'>clear " . $vertical . "</a></span>"; 

Thanks in advance!

0
php
Dec 22 '13 at 15:56
source share
3 answers

You cannot call PHP functions after the page has finished loading. PHP is a server technology and runs on a server, not on a client computer. This means that you cannot call the PHP function without sending the details to the script.

If you try to call the unset function when the user clicks the link, you can create a link to the script where you cancel the $_SESSION variable:

 <span><a href='somepage.php?somevar=42'>foo</a></span> 

When the user clicks on the link, they will be sent to somepage.php . Now you can check if the somevar key is somevar , and then unset session in the script:

 <?php session_start(); if (isset($_GET['somevar'])) { unset($_SESSION['vertical']); } 

If you want to do this without refreshing the page, you can take a look at AJAX .

+2
Dec 22 '13 at 16:08
source share

When writing unset($_SESSION['x']) x in the session will be deleted. It will be executed immediately. You must provide a link to the page that will make the deletion.

If you want to clear the session variable on the same page, you need to make an AJAX call on another page with the unset($_SESSION['x']) code unset($_SESSION['x']) .

0
Dec 22 '13 at 16:04
source share

You need to do the following:

  <a href="sessiondestroy.php">Clear " . $vertical . "</a> and in clearsession.php write: <?php session_start(); unset($_SESSION['vertical']); ?> Let me know what happened? 
0
Dec 22 '13 at 16:08
source share



All Articles