PHP标头没有重定向到home.php

时间:2012-08-24 09:47:21

标签: php

我正在努力使标题重定向,它在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

6 个答案:

答案 0 :(得分:1)

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

if(isset($_SESSION['id']))
{
 header("Location: home.php");
}

答案 1 :(得分:0)

require_once 'global.php';应排在首位,因为session_start()

require_once 'global.php';
if(isset($_SESSION['id'])) {
 header("Location: home.php");
}

答案 2 :(得分:0)

在其他任何内容之前加入global.php,如果这是您保留session_start的位置。

答案 3 :(得分:0)

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

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

<?php
  require_once 'global.php';

  if(isset($_SESSION['id']))
  {
    header("Location: home.php");
  }
?>

答案 4 :(得分: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,否则脚本将继续执行。

答案 5 :(得分:0)

如果更改这些行怎么办:

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

用这些:

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