自我形式动作在代码中显示PHP代码

时间:2017-04-22 06:46:39

标签: php html5 forms

我正在开发有关注册的PHP代码。 我开始知道自我形式行为是安全的,而不是去另一个表格行动页面。 当我创建表单操作时,值会插入到数据库中,但我的问题又是PHP CODE也在上面的部分中打印。

这是我的PHP代码:

    <?php
      ob_start();
      session_start();
      if( isset($_SESSION['user'])!="" ){
      header("Location: index.php");
      }
    include 'dbconfig.php';
     $error = false;
     extract($_POST);
        error_reporting(0);

     ## :: Registration :: ##

    if(isset($_POST['submit']))
       {
    $fname          =   $_POST['fname'];    //gets firstname from frontend
    $lname          =   $_POST['lname'];    //gets lastname from frontend
    $email          =   $_POST['email'];    //gets email from frontend
    $password       =   $_POST['password']; //gets password from frontend
    $cpassword      =   $_POST['cpassword'];    //gets Confirm password from frontend
    $mobile         =   $_POST['mobile'];   //gets mobile from frontend
    $class          =   $_POST['class'];    //gets class from frontend
    $remote         =   $_SERVER['REMOTE_ADDR']; //gets the ip address from user


 $reg_query = mysqli_query($conn, "INSERT INTO `at_reg_user`(`fname`, `lname`, `email`, `password`, `cpassword`, `mobile`, `class`, `ipaddress`) VALUES ('$fname','$lname','$email','$password1','$cpassword1','$mobile','$class','$remote')");
      if ($reg_query) {
     header('Location: index');
     $errMSG = "Successfully registered, you may login now";
    unset($fname);
      unset($lname);
   unset($email);
   unset($password);
    unset($cpassword);
   unset($mobile);
   unset($class);

      } else {
    $errMSG = " Not Successfully registered, you may login now";
     }
 }


 }
  ?>

这是我的表单操作代码

        <form  method="post" action='<?php echo htmlentities($_SERVER["PHP_SELF"]);?>' >
    <div class="form-group"> <span class="text-danger"><?php echo $fnameError;  ?></span>
        <label class="sr-only" for="form-fname">First name</label>
        <input type="text" name="fname" placeholder="First name..." class="form-first-name form-control" id="fname">
    </div>
    <div class="form-group"><span class="text-danger"> <?php echo $lnameError;  ?></span>
        <label class="sr-only" for="form-last-name">Last name</label>
        <input type="text" name="lname" placeholder="Last name..." class="form-last-name form-control" id="lname">
    </div>
    <div class="form-group"><span class="text-danger"> <?php echo $emailError; ?></span>
        <label class="sr-only" for="form-email">Email</label>
        <input type="email" name="email" placeholder="Email..." class="form-email form-control" id="email">
    </div>
    <div class="form-group"><span class="text-danger"> <?php echo $passwordError; ?></span>  <span class="text-danger"> <?php echo $passwordmError; ?></span>
        <label class="sr-only" for="form-password">Password</label>
        <input type="password"  name="password" placeholder="Password..." class="form-password form-control" id="password">
    </div>
    <div class="form-group"><span class="text-danger"><?php echo $cpasswordError; ?></span>  <span class="text-danger"> <?php echo $cpasswordmError; ?></span>   <span class="text-danger"> <?php echo $mcpasswordError; ?>
        <label class="sr-only" for="form-confirm-password">Confirm Password</label>
        <input type="password"  name="cpassword" placeholder="Confirm Password..." class="form-cpassword form-control" id="cpassword">
    </div>
    <div class="form-group"><span class="text-danger"><?php echo $mobileError; ?></span>
        <label class="sr-only" for="form-mobile">Mobile Number</label>
        <input type="number" maxlength="10" name="mobile" placeholder="Mobile Number..." class="form-mobile form-control" id="mobile">
    </div>
    <div class="form-group"><span class="text-danger"><?php echo $classError; ?></span>
        <select name="class"  for="form-class" class="form-class form-control id" id="class">
            <option selected="true" disabled="disabled">------Select Class------</option>
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>
            <option value="4">4</option>
            <option value="5">5</option>
            <option value="6">6</option>
            <option value="7">7</option>
            <option value="8">8</option>
            <option value="9">9</option>
            <option value="10">10</option>
        </select>
    </div>

    <button type="submit" class="btn" name="submit" id="submit">Sign me up!</button>
</form>

问题是&#34;它再次成功重定向到索引页面,但上面的php代码正在显示

1 个答案:

答案 0 :(得分:0)

从php文件中删除&#34; ob_start();&#34; ..