我有以下两段代码正常工作..
<?php session_start();
$query = '23';
$_SESSION['testing'] = $query;
echo $_SESSION['testing'] . "<html><br></html>";
echo "<html><a href='http://www.nhl-statistics.com/scripts/test2.php'>click</a></html>"
?>
这个链接也是..
<?php session_start();
echo $_SESSION['testing'] . "<html><br></html>";
echo "<html><a href='http://www.nhl-statistics.com/scripts/test2.php'>click</a></html>"
session_destroy();
?>
然而..我将发布两个页面,其中相同的方法不起作用,我希望你们能告诉我为什么
<?php session_start();
?>
<script type="text/javascript">
function destroyit()
{
<?php session_destroy(); ?>
}
</script>
</head>
<body>
<div id="page" align="center">
<div id="header">
<div id="companyname" align="left"></div>
<div align="right" class="links_menu" id="menu"><a href="/index2.php">Home</a> | <a href="logged.php">Log In</a> | <a href="#">Products</a> | <a href="#">Our Services</a> | <a href="#">Contact Us</a> </div>
</div>
<br />
<div id="content">
<div id="leftpanel">
<div class="table_top">
<div align="center"><span class="title_panel">News</span> </div>
</div>
<div class="table_content">
<div class="table_text">
<p class="news_date">blah.com<br />
blah.<br />
</p>
</div>
</div>
<div class="table_bottom">
<img src="images/table_bottom.jpg" width="204" height="23" border="0" alt="" /><br />
</div>
<div class="table_top"></div>
<div class="table_content">
<div class="table_text">
<html><form action="?" method="post">
Username: <input type="text" name="uname" /><br />
Password: <input type="text" name="pword" /><br />
<input type="submit" />
</form> </html>
</div>
</div>
<div class="table_bottom">
<img src="images/table_bottom.jpg" width="204" height="23" border="0" alt="" />
</div>
<div class="footer">
<p><br />
<a href="#">Home</a> | <a href="#">About Us</a> | <a href="#">Products</a> | <a href="#">Our Services</a> | <a href="#">Contact Us</a> | blah.com</a>.
</p>
</div>
</div>
<div id='contenttext' class='contenttext'>
<?php $username="a75sdadmin";
$password="1dBasdns2";
$database="a75asdesting";
$pword = $_REQUEST['pword'];
$uname = $_REQUEST['uname'];
mysql_connect('myssd.com',$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = mysql_query("SELECT * FROM `username` WHERE `password` = '$pword' AND `username` = '$uname'");
$exsists = 0;
WHILE($rows = mysql_fetch_array($query)){
$exsists = 1;
break;
}
if ($exsists){
$_SESSION['usern23']=$uname;
$_SESSION['logged']=1;
header('Location: logged2.php');
}
mysql_close();
?>
</div>
</body>
</html>
会话变量由于某种原因不会发布到此页面..(logged2)
<?php session_start();
echo 'welcome, ' . $_SESSION['usern23'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta name="author" content="Wink Hosting (www.winkhosting.com)" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>Aqua</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="/classarray.js"></script>
<script type="text/javascript">
function doit()
{
var x=document.getElementById("multi2");
var xindex = x.selectedIndex;
var y=document.getElementById("multi3");
var yindex = y.selectedIndex;
var class4 = x.options[xindex].text;
var class5 = y.options[yindex].text;
var link = "index3.php?rav=" + class4 + "&pun=" + class5
window.open(link, '_self');
}
</script>
<script type="text/javascript">
function destroyit()
{
<?php session_destroy(); ?>
}
</script>
<script type="text/javascript">
$(document).ready(function(){
$("button.yo1").click(function rock(){
$("div.class1").html("<h1> </h1><h1><br /><select id='multi2' style='width:100px;'></select><br /></h1><button id='yo8' class='yo8'>Pick Class</button>")
childless();
$("button.yo8").click(function rock2(){
$("div.class3").html("<h1> </h1><h1><br /><select id='multi3'><option>random teacher</option></select><br /></h1><button id='yo9' class='yo9'>Pick Teacher</button>")
$("button.yo9").click(function rock3(){
$("div.golow").html("<button onClick='doit();'>click for notes</button>")
$("button.yo10").click(function rock3(){
$("div.contenttext").html("<div id='yi'></div>")
});
});
});
});
});
</script>
</head>
<body>
<div id="page" align="center">
<div id="header">
<div id="companyname" align="left"></div>
<div align="right" class="links_menu" id="menu"><a href="index2.php">Home</a> | <a href="/logged.php">Log In</a> | <a href="#">Products</a> | <a href="#">Our Services</a> | <a href="#">Contact Us</a> </div>
</div>
<br />
<div id="content">
<div id="leftpanel">
<div class="table_top">
<div align="center"><span class="title_panel">News</span> </div>
</div>
<div class="table_content">
<div class="table_text">
<p class="news_date">sdfgass.com<br />
dsf<br />
</p>
</div>
</div>
<div class="table_bottom">
<img src="images/table_bottom.jpg" width="204" height="23" border="0" alt="" /><br />
</div>
<div class="table_top"></div>
<div class="table_content">
<div class="table_text">
<?php
error_reporting(E_ALL);
echo $usern . '<html><br></html>';
echo '<html><a href="logged.php" onclick="destroyit();" >logout</a></html>';
?>
</div>
</div>
<div class="table_bottom">
<img src="images/table_bottom.jpg" width="204" height="23" border="0" alt="" />
</div>
<div class="footer">
<p><br />
<a href="#">Home</a> | <a href="#">About Us</a> | <a href="#">Products</a> | <a href="#">Our Services</a> | <a href="#">Contact Us</a> | Sasd.com</a>.
</p>
</div>
</div>
<div id='contenttext' class='contenttext'>
you are now logged in
</div>
</body>
</html>
答案 0 :(得分:1)
<script type="text/javascript">
function destroyit()
{
<?php session_destroy(); ?>
}
</script>
这绝不会奏效。 PHP很快就会在服务器上执行,早在浏览器接触到生成的页面之前。如果您查看页面的来源,您将看到上面的代码已生成:
<script type="text/javascript">
function destroyit()
{
}
</script>
基本上你正在开始一个会话,然后立即摧毁它。如果你想从客户端javascript中销毁一个会话,你将不得不进行一次AJAX调用(或者提交一个表单,点击一个链接等等)来调用一个执行销毁的PHP脚本。 / p>