【问题标题】:SESSION paramaters not carrying over to other pagesSESSION 参数没有传递到其他页面
【发布时间】:2014-09-29 08:02:15
【问题描述】:

我有一个登录页面,它在登录时设置了一个 _SESSION 参数“id”。登录后,用户被重定向到 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&#40;".$messages."&#41;</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> &nbsp&nbsp&nbsp 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&#40;$messages&#41; </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);
?>

【问题讨论】:

  • account.phpindex.php 在同一台服务器上吗?
  • 是的,他们是@antoine129
  • 使用一个好的IDE,这样你就可以轻松地进行调试,它可以验证手头的错误..$_SESSSION你在开玩笑吗?
  • 所以从登录页面,你去索引页面,然后你去帐户页面。您的索引文件的代码是什么?在我看来,这两个页面之间的差异可以帮助您调试问题。
  • index.php 代码已添加@wick3d

标签: php session


【解决方案1】:

应该是$_SESSION 而不是$_SESSSION

您应该会看到具有正确错误报告级别的警告,例如

error_reporting(E_ALL);

【讨论】:

    【解决方案2】:

    你在使用 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。

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 2022-01-18
      • 2017-05-08
      • 2012-06-17
      • 1970-01-01
      相关资源
      最近更新 更多