0

我正在尝试使标题重定向,它就像 2 天前一样工作,我不知道我做了什么来打破它。它现在只是重新加载页面但启动会话。

这是代码,

<?php

if(isset($_SESSION['id']))

{
 header("Location: home.php");
}

require_once 'global.php';

?>

<head>
 <title>BookWizard - Join Now!</title>




<meta charset="utf-8" />
    <meta name="description" content="FORMETER jQuery plugin" />
    <link rel="stylesheet" href="//fonts.googleapis.com/css?family=Marmelad">
    <link rel="stylesheet" href="/_secure/stylesheets/style.css" />
    <link rel="stylesheet" href="css/formeter.1.0.css" />
    <!--[if IE]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
                  </script><![endif]-->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">

    </script>
    <script src="javascript/formeter.1.0.min.js"></script>
    <script>
        $(function(){
            $('#percentage').formProgress({
                'speed' : 800,
                'style' : 'green',
                'bubble' : true,
                'selector' : '.recommended'
            });
        });
    </script>
</head>
<body>
    <header>
        <a class="logo" href="index.html"><!-- --></a>
    </header>
    <div id="wrapper">




        <div class="clear"></div>
        <div id="container">

            <div id="registercontainerno">

         <img src="images/logo.png" class="frontpagelogo">


   <div class="meter" style="position:absolute; margin-left: 50px; margin-top: 20px;">
                    <div id="percentage" data-percent=""></div>
                </div>



      <br><br><br><br>

            <h2 class="paragraphf">BookWizard Information</h2>


<?php
if(isset($_POST['password']))
{
$firstname = mysql_real_escape_string($_POST['firstname']);
$email = mysql_real_escape_string($_POST['email']);
$username = mysql_real_escape_string($_POST['username']);
$password = ($_POST['password']);

$lastname = mysql_real_escape_string($_POST['lastname']);
if(mysql_num_rows(mysql_query("SELECT * FROM users WHERE username='$username'")) == 0) {
mysql_query("INSERT INTO users (firstname, email, avatar, password, username, lastname)
  VALUES ('$firstname', '$email', 'http://book-wizard.net/images/default.png',   
 '$password',   '$username', '$lastname')");
$_SESSION['id'] = $username;
Header('Location: home.php');
}
else {
echo "Error, Please try again later.";
die();
}
}
?>

            <form method="post">
<img src="/images/regi.jpg" align="right" style="margin-top: -100px; margin-right:
10px; border-radius: 5px;">
                <p class="paragraphf">
                    <label>Username</label>
                    <input type="text" name="username" class="recommended" />
                </p>
                <p class="paragraphf">
                <label>Password</label> 

 <input type="password" name="password" class="recommended" />      

                <h2 class="paragraphf">Personal Information</h2>
                <p class="paragraphf">
                    <label>First name</label>
                    <input type="text" name="firstname" class="recommended" />
                </p>
                <p class="paragraphf">
                    <label>Last name</label>
                    <input type="text" name="lastname" class="recommended" />
                </p>

                <h2 class="paragraphf">Contact Information</h2>
                <p class="paragraphf">
                <label>Email Address</label>
        <input type="text" name="email" class="recommended" />






                <p class="input_bg">
    <input type="submit" value="SIGN UP" class="buttonr blue"><a href="index.php"  

 class="buttonr orange">CANCEL</a>
                    <br><br>
                </p>
            </form>

            <div class="clear"></div>

        </div> <!-- END container -->
    </div> <!-- END wrapper -->

    </body>
</html>

在 global.php 中它确实有一个 session_start();... 不确定问题:S

4

6 回答 6

1

在这段代码之前,您是否在页面顶部包含 session_start() :

if(isset($_SESSION['id']))
{
 header("Location: home.php");
}
于 2012-08-24T09:48:16.177 回答
0

似乎您想将所有还没有会话(第一次访问)的用户重定向到home.php,但这不是要走的路。您无法$_SESSION在通过调用填充之前检查变量session_start(),并且在调用 session_start() 之后,您将始终在变量中拥有一个会话 ID。

您可以做的是在脚本开始时建立一个会话,然后检查是否已经设置了某个变量。

session_start();
if (!isset($_SESSION['already_been_here']))
{
  $_SESSION['already_been_here'] = true;
  header('Location: home.php', true, 303);
  exit;
}

重定向后不要忘记调用exit,否则脚本会继续执行。

于 2012-08-24T11:12:13.557 回答
0

移动require_once 'global.php';到页面顶部。

您需要先调用session_start(),然后才能使用会话变量!

<?php
  require_once 'global.php';

  if(isset($_SESSION['id']))
  {
    header("Location: home.php");
  }
?>
于 2012-08-24T09:54:59.177 回答
0

require_once 'global.php';应该在顶部,因为有session_start().

require_once 'global.php';
if(isset($_SESSION['id'])) {
 header("Location: home.php");
}
于 2012-08-24T09:49:57.493 回答
0

包括global.php在其他任何事情之前,如果那是您保留session_start.

于 2012-08-24T09:50:26.780 回答
0

如果您更改这些行怎么办:

{
 header("Location: home.php");
}

用这些:

{
 header("Location: home.php");
 exit();
}

?

于 2012-08-24T11:27:13.780 回答