我正在爲我的網站管理頁面,但我發現了一個問題。我的代碼與Wamp完美結合(並且仍然在Chrome中工作,但不適用於其他人或其他瀏覽器)。 所以,問題是我的網站在頭重定向後不保存會話變量。 (還有一件事我不明白的是,如果我把芯片或退出的頭後,頁面只是變成空白,你必須手動刷新。不過這只是在網絡服務器。)PHP會話 - 不保存
<?php
session_start();
$log = $_GET['logout'];
$error = false;
//logout
if($log==true){
unset($_SESSION['user']);
header('Location: '.$_SERVER['PHP_SELF']);
//die;
}
//login
if (isset($_POST['submit']) && !isset($_SESSION['user'])){
$file="login.xml";
$xml=simplexml_load_file($file) or die ("Unable to load XMl file!");
$nope=true;
$givenuser = $_POST['username'];
$givenpass = md5($_POST['password']);
foreach($xml->user as $user){
$username = $user->username;
$password = $user->password;
if ($givenuser == $username && $givenpass == $password){
$_SESSION['user'] = $givenuser;
$nope =false;
header('Location: '. $_SERVER['PHP_SELF']);
//die();
}
}
if($nope) $error = true;
}
//succesful login
if (isset($_SESSION['user'])){
?>
<html>
<header><link rel="stylesheet" href="style.css" type="text/css" /></header>
<body>
<h1>
<?php echo "Welcome ".$_SESSION['user']; ?>
</h1>
<div>
<a href="?">New page</a>
<a href="?logout=true">Logout</a>
</div>
<div id="menus">
Pages:</br>
<?php
$file="../pages.xml";
$xml=simplexml_load_file($file) or die ("Unable to load XMl file!");
$index = 0;
foreach($xml->xpath('//oldalneve') as $pagename){
echo '<a href="?id='.$index.'">'.$pagename.'</a></br>';
$index=$index+1;
}
?>
</div>
<?php
if(!is_numeric($_GET['id'])){
$id=-1;
}else{
$id=(int)$_GET['id'];
}
if($id!=-1){
$id=(int)$_GET['id'];
if(isset($_POST['name']) && isset($_POST['body'])){
if($_REQUEST['name'] !="" && $_REQUEST['body']!=""){
$file="../pages.xml";
$xml= simplexml_load_file($file) or die ("Unable to load XML file!");
$name = $_POST['name'];
$body = $_POST['body'];
$xml->page[$id]->oldalneve = $name;
$xml->page[$id]->body = $body;
$xml->asXML('../pages.xml');
header('Location: '.$_SERVER['PHP_SELF'].'?id='.$id);
//die;
}
}
$file="../pages.xml";
$xml= simplexml_load_file($file);
$nam=$xml->page[$id]->oldalneve;
$val=$xml->page[$id]->body;
?>
<div style="float:left;">
<form name="input" method="post" action="" >
<label>Name</label></br>
<input type="text" name="name" style="width:250px" value="<?php echo $nam;?>"></br>
<label>Body</label></br>
<textarea cols="28" rows="10" name="body"><?php echo $val;?></textarea></br>
<input type="submit" name="submit" value="Küldés">
</form>
</div>
</body>
</html>
<?php
}else{
?>
<div style="float:left;">
<form name="input" method="post" action="" >
<fieldset><legend>Add page</legend>
<label>Name</label></br>
<input type="text" name="name" style="width:250px"></br>
<label>Body</label></br>
<textarea cols="28" rows="10"v name="body"></textarea></br>
<input type="submit" name="submit" value="Küldés">
</fieldset>
</form>
</div>
</body>
</html>
<?php
if(isset($_POST['submit'])){
if($_REQUEST['name'] !="" && $_REQUEST['body']!=""){
$file="../pages.xml";
$xml= simplexml_load_file($file) or die ("Unable to load XML file!");
$page=$xml->addChild('page');
$name = $_POST['name'];
$body = $_POST['body'];
$page->addChild('oldalneve', $name);
$page->addChild('body', $body);
$xml->asXML('../pages.xml');
header('Location: '.$_SERVER['PHP_SELF']);
//die;
}
}
}
}else
{
showLogin($error);
}
function showLogin($error){?>
<form action="" method='post'>
<label for="username">username</label>
<input type="text" name="username" id="username"></br>
<label for="password">password</label>
<input type="password" name="password" id="password"></br>
<?php
if($error){
echo "Invalid username or password".'</br>';
}
?>
<input type="submit" name="submit" value="submit">
</form>
<?php }
?>
的用戶可以登錄並查看界面,但使用其中一個鏈接會將其重定向到登錄屏幕(會話用戶不會保存)。
這裏是phpinfo()函數:
session.auto_start Off Off
session.bug_compat_42 On On
session.bug_compat_warn On On
session.cache_expire 180 180
session.cache_limiter nocache nocache
session.cookie_domain no value no value
session.cookie_httponly Off Off
session.cookie_lifetime 0 0
session.cookie_path//
session.cookie_secure Off Off
session.entropy_file no value no value
session.entropy_length 0 0
session.gc_divisor 100 100
session.gc_maxlifetime 1440 1440
session.gc_probability 1 1
session.hash_bits_per_character 4 4
session.hash_function 0 0
session.name PHPSESSID PHPSESSID
session.referer_check no value no value
session.save_handler files files
session.save_path /tmp /tmp
session.serialize_handler php php
session.use_cookies On On
session.use_only_cookies Off Off
session.use_trans_sid 0 0
我認爲你有這個問題:if($ log == true)。如果您嘗試使用「===」會發生什麼情況? – c4k
這聽起來像* *不能修改標題信息 - 標題已經由*類問題發送。你有任何PHP錯誤/警告? (如果被抑制,使用'ini_set(「display_errors」,true)'或檢查源代碼,因爲它們可能在''之前打印,因此在頁面上不可見) –
Chafik:with ===註銷不一切工作。 solymosi:實際上我確實得到了那個錯誤。我修改了代碼,因此每個打印的代碼都在標記中。 如果我激活模具;代碼我仍然得到錯誤,沒有任何顯示。 –