样式化具有PHP的页面

时间:2013-04-20 02:20:41

标签: php html css error-handling include

我正在尝试创建一个用户注册表单,在用户注册后向用户发送确认电子邮件,当我在本地托管该网站时,一切正常。但是,当我在网络(hostgator)上托管它时,我收到以下错误:

  

警告:session_start()[function.session-start]:无法发送会话   缓存限制器 - 已发送的标头

我知道部分之前已经多次回答了 - 是的,我已经删除了我的PHP代码上方区域的所有文本,错误就消失了。

但是,如果我不能在此之上添加任何内容,那么我如何在我的网站上使用此代码并仍然样式化页面?我想使用css样式表,但是现在我不能使用任何内容,甚至在没有看到错误的情况下声明我的doctype。

我尝试创建一个程式化的html文件然后使用php include函数来包含我的php代码,但这不起作用。

我可以做些什么来对我的网页进行样式化,并且仍然可以在同一页面上使用此PHP代码而不会出错?另外,为什么代码在我的本地服务器上正常工作,但是当我在hostgator上传它时却没有?

下面是我用于导致错误的页面的php代码。有什么想法吗?

的index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
 session_start();
 if(isset($_SESSION['error']))
 {
  echo '<p>'.$_SESSION['error']['username'].'</p>';
  echo '<p>'.$_SESSION['error']['email'].'</p>';
  echo '<p>'.$_SESSION['error']['password'].'</p>';
  echo '<p>'.$_SESSION['error']['gender'].'</p>';
  unset($_SESSION['error']);
 }
?>
<div class="signup_form">
<form action="register.php" method="post" >
 <p>
  <label for="username">User Name:</label>
  <input name="username" type="text" id="username" size="30"/>
 </p>
 <p>
  <label for="email">E-mail:</label>
  <input name="email" type="text" id="email" size="30"/>
 </p>
 <p>
  <label for="password">Password:</label>
  <input name="password" type="password" id="password" size="30 "/>
 </p>
   <p>
    <label for="gender">Gender: </label>
    <select name="gender" id="gender">
      <option value="NULL" selected>Select...</option>
      <option value="Male">Male</option>
      <option value="Female">Female</option>
    </select>
  </p>
  <p>
  <label for="postalcode">Postal Code:</label>
  <input name="postalcode" type="text" id="postalcode" size="15"/>
 </p>
 <p>
  <input name="submit" type="submit" value="Submit"/>
 </p>
</form>
</div>


</body>
</html>

register.php

<?php
session_start();
include('configdb.php');
if(isset($_POST['submit']))
{
 //whether the username is blank
 if($_POST['username'] == '')
 {
  $_SESSION['error']['username'] = "User Name is required.";
 }
 //whether the email is blank
 if($_POST['email'] == '')
 {
  $_SESSION['error']['email'] = "E-mail is required.";
 }
 else
 {
  //whether the email format is correct
  if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9._-]+)+$/", $_POST['email']))
  {
   //if it has the correct format whether the email has already exist
   $email= $_POST['email'];
   $sql1 = "SELECT * FROM user WHERE email = '$email'";
   $result1 = mysqli_query($mysqli,$sql1) or die(mysqli_error());
   if (mysqli_num_rows($result1) > 0)
            {
    $_SESSION['error']['email'] = "This Email is already used.";
   }
  }
  else
  {
   //this error will set if the email format is not correct
   $_SESSION['error']['email'] = "Your email is not valid.";
  }
 }
 //whether the password is blank
 if($_POST['password'] == '')
 {
  $_SESSION['error']['password'] = "Password is required.";
 }
  //if the error exist, we will go to registration form
 if(isset($_SESSION['error']))
 {
  header("Location: index.php");
  exit;
 }
 else
 {
  $username = $_POST['username'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $gender = $_POST['gender'];
  $postalcode = $_POST['postalcode'];
  $com_code = md5(uniqid(rand()));

  $sql2 = "INSERT INTO user (username, email, password, com_code, gender, postalcode) VALUES ('$username', '$email', '$password', '$com_code', '$gender', '$postalcode')";
  $result2 = mysqli_query($mysqli,$sql2) or die(mysqli_error());

  if($result2)
  {
   $to = $email;
   $subject = "Confirmation from TutsforWeb to $username";
   $header = "TutsforWeb: Confirmation from TutsforWeb";
   $message = "Please click the link below to verify and activate your account: ";
   $message .= "http://www.emotionpool.com/SWIFT/confirm.php?passkey=$com_code";

   $sentmail = mail($to,$subject,$message,$header);

   if($sentmail)
            {
   echo "Your Confirmation link Has Been Sent To Your Email Address.";
   }
   else
         {
    echo "Cannot send Confirmation link to your e-mail address";
   }
  }
 }
}
?>

2 个答案:

答案 0 :(得分:0)

Session_start必须在任何输出之前。

 <?php
 session_start();?>
    <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<?php
 if(isset($_SESSION['error']))
 {
  echo '<p>'.$_SESSION['error']['username'].'</p>';
  echo '<p>'.$_SESSION['error']['email'].'</p>';
  echo '<p>'.$_SESSION['error']['password'].'</p>';
  echo '<p>'.$_SESSION['error']['gender'].'</p>';
  unset($_SESSION['error']);
 }
?>
<div class="signup_form">
<form action="register.php" method="post" >
 <p>
  <label for="username">User Name:</label>
  <input name="username" type="text" id="username" size="30"/>
 </p>
 <p>
  <label for="email">E-mail:</label>
  <input name="email" type="text" id="email" size="30"/>
 </p>
 <p>
  <label for="password">Password:</label>
  <input name="password" type="password" id="password" size="30 "/>
 </p>
   <p>
    <label for="gender">Gender: </label>
    <select name="gender" id="gender">
      <option value="NULL" selected>Select...</option>
      <option value="Male">Male</option>
      <option value="Female">Female</option>
    </select>
  </p>
  <p>
  <label for="postalcode">Postal Code:</label>
  <input name="postalcode" type="text" id="postalcode" size="15"/>
 </p>
 <p>
  <input name="submit" type="submit" value="Submit"/>
 </p>
</form>
</div>


</body>
</html>

答案 1 :(得分:0)

您可以在html声明

之上声明php代码
<?php session_start(); ?>
<!doctype html>
<html><head>
... your headers && some php codes
</head>
<body> 
... your body && some php codes
</body>
</html>