我正在尝试调用函数 session_destroy()
并打印字符串但没有成功。我尝试关注这篇文章calling-a-php-function-within-an-a-tag-anchor-tag并重定向到不同的页面,但我收到错误
Warning: session_destroy(): Trying to destroy uninitialized session
显然,如果我转到另一个页面,则没有 session 被初始化为被销毁。我想我应该使用像
这样的函数function restartSession{
session_destroy();
print "SESSION has been destroyed - all session data deleted";
}
并使用<form action="<?php $_SERVER['PHP_SELF'] ?>" method="GET">
,但是如何使用一个 php 文件来做到这一点?
最佳答案
您可以使用普通的 anchor 标记来进行GET
:
<a href="http://localhost/lab5.php?restartSession=true">restart Session</a>
然后你也可以将其放入你的index.php中:
if(isset($_GET["restartSession"])){
restartSession();
}
function restartSession(){
if(session_id() == '') { //check if Session already exists
print "Only start Session";
session_start();
}
else {
session_destroy();
session_start();
print "Session restarted";
}
}
关于php - 使用 HTML <form action method=GET> 在 PHP 页面中重新启动 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42747033/