登錄.php <?php ob_start(); session_start(); include '../config.php'; if( (isset($_SESSION['can-id']) )) { header('location: ../home/profile.php'); } if(isset($_POST['can-login'])) { $email=$_POST['email']; $password=$_POST['password']; $sql="SELECT * FROM `user_credentials` WHERE `email`=:email AND `password`=:password"; $pdoResult=$conn->prepare($sql); $pdoExec=$pdoResult->execute(array(":email"=>$email,":password"=>$password)); $pdoResult->setFetchMode(PDO::FETCH_ASSOC); $count=0; $uid=''; while ($r=$pdoResult->fetch()) { # code... $count+=1; $uid=$r['email']; } if ($count==1) { # code... $_SESSION['can-id']=$uid; header('location: ../home/profile.php'); } else { $_SESSION['error']="login failed"; } } ?><html>....</html>配置文件.php<?php ob_start(); session_start(); if (!(isset($_SESSION['can-id']))) { # code... header('location: ../login/'); } else { $cid=$_SESSION['can-id']; }?><h1 ><?php echo $cid;?></h1>這是我在登錄頁面后的代碼被重定向到 profile.php 頁面但在配置文件頁面會話變量沒有打印我不知道為什么但每次登錄時都沒有出現這個問題它有時會發生所以我可以'找不到問題所在。任何人都知道請幫助我解決問題。
1 回答

狐的傳說
TA貢獻1804條經驗 獲得超3個贊
從你的 login.php 中刪除 ob_start() 不要把 session_start() 放在你所有的文件中
e.g login.php, profile.php, etc
而是將其添加到您的 config.php 中,例如:
<?php
session_start();
//.. config variables here
然后,在您的 profile.php 中也包含 config.php。
- 1 回答
- 0 關注
- 108 瀏覽
添加回答
舉報
0/150
提交
取消