我有一个登录页面,在登录时设置'id'的_SESSION参数。登录后,用户被重定向到index.php页面,此时会话参数仍然设置,我知道这是因为我可以用PHP打印它。但是,当导航到account.php页面时,参数会丢失,我不知道为什么。
此处询问的大多数其他问题都是因为没有使用session_start但我在account.php页面上使用了这个问题。
任何解决这个问题的帮助都会很感激。
account.php页面的代码如下:
<?php session_start(); ?>
<?php
$con=mysqli_connect("localhost","tkernick96","Tylerkernick1996","users");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_SESSSION['id'];
$query = mysqli_query($con,"SELECT * FROM users WHERE id = $id");
$row = mysqli_fetch_array($query,MYSQLI_ASSOC);
$firstname = $row ['firstname'];
$lastname = $row ['lastname'];
$email = $row ['email'];
$age = $row ['age'];
$nationality = $row ['nationality'];
$language1 = $row ['language1'];
$language2 = $row ['language2'];
$language3 = $row ['language3'];
$language4 = $row ['language4'];
$language5 = $row ['language5'];
$form = "<div id='header'>
<img src='logo.png' id='logo'>
<div id='headerdiv'>
<table id='login'>
<tr>
<td style='padding-right: 20px;'>
<h2 style='margin-top: 0px'>No account?</h2>
<h3 style='margin-top: 0px'><a href='signup.php'>Sign Up Now!</a></h3>
</td>
<td style='padding-left: 20px; border-left: 1px solid #838383'>
<h3>Login:</h3>
<form method='post' action='login.php'>
<input id='logininput' type='email' name='email' placeholder='Email'>
<br>
<input id='logininput' type='password' name='password' placeholder='Password'>
<input type='submit' name='login' value='Login'>
</form>
<p id='p1'><a href='forgotdetails.php'>Forgotten Details?</a></p>
</td>
</tr>
</table>
</div>
</div>";
$account = "<div id='header'>
<img src='logo.png' id='logo'>
<div id='headerdiv'>
<table id='accounttable'>
<tr>
<td id='account'>
<p id='p2'><a href='account.php'>My Account</a></p>
<p id='p2'><a href='account/companions.php'>My Companions</a></p>
<p id='p2'><a href='account/messages.php'>Messages(".$messages.")</a></p>
<p id='p2'><a href='logout.php'>Sign Out</a></p>
</td>
</tr>
</table>
</div>
</div>
<table id='profiletable'>
<tr>
<td id='profiletabletd1' rowspan='2'>
<img src='images/avatar.png' id='profileimage'>
</td
<td id='profiletabletd1'><p id='profilename'>".$firstname." ".$lastname.", ".$age."</p></td>
</tr>
<tr>
<td id='profiletabletd3'><p id='profileinfo'>Nationality: <b>".$nationality."</b>     Languages Spoken: <b>".$language1." ".$language2." ".$language3." ".$language4." ".$language5."</b></p></td>
</tr>
</table>
";
if ($id == "") {
$output = $form;
}
else {
$output = $account;
}
mysqli_close($con);
?>
的index.php:
<?php session_start(); ?>
<?php
$con=mysqli_connect("localhost","tkernick96","Tylerkernick1996","users");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id = $_SESSION['id'];
$form = "<table id='login'>
<tr>
<td style='padding-right: 20px;'>
<h2 style='margin-top: 0px'>No account?</h2>
<h3 style='margin-top: 0px'><a href='signup.php'>Sign Up Now!</a></h3>
</td>
<td style='padding-left: 20px; border-left: 1px solid #838383'>
<h3>Login:</h3>
<form method='post' action='login.php'>
<input id='logininput' type='email' name='email' placeholder='Email'>
<br>
<input id='logininput' type='password' name='password' placeholder='Password'>
<input type='submit' name='login' value='Login'>
</form>
<p id='p1'><a href='forgotdetails.php'>Forgotten Details?</a></p>
</td>
</tr>
</table>";
$account = "<table id='accounttable'>
<tr>
<td id='account'>
<p id='p2'><a href='account.php'>My Account</a></p>
<p id='p2'><a href='account/companions.php'>My Companions</a></p>
<p id='p2'><a href='account/messages.php'>Messages($messages) </a></p>
<p id='p2'><a href='logout.php'>Sign Out</a></p>
</td>
</tr>
</table>";
if ($id == "") {
$output = $form;
}
else {
$output = $account;
}
mysqli_close($con);
?>
答案 0 :(得分:2)
它应该是$_SESSION
而不是$_SESSSION
!
您应该看到具有正确错误报告级别的警告,例如
error_reporting(E_ALL);
答案 1 :(得分:0)
您使用的是IIS吗? 如果是,则应检查php.ini文件(在c:\ php或c:\ windows下),找到session.save_path参数,并确保指向存在的文件夹。 然后编辑该文件夹的权限并为IUSR用户授予读/写权限。
如果您正在使用Apache,您应该找到启动守护程序的用户(您可以在/etc/apache2/apache.conf中找到它),然后确保该用户在该文件夹中具有r / w权限那个php.ini session.save_path指向。
之后重启IIS或Apache。
希望它有所帮助。