dprntkxh703029 2013-11-09 20:59
浏览 28
已采纳

被发送到网站的不同部分

I'm working on a register form, for my website.

<?php
  include('config.php');

  if(isset($_SESSION['username'])) {
    header('Location:index.php');
  }

  if(isset($_POST['submit-registerform'])) {
    Register();
  }

  function Register() {
    if(!empty($_POST['username']) &&
       !empty($_POST['password']) &&
       !empty($_POST['lastname']) &&
       !empty($_POST['email'])) {
         // Database Connection:
         require('config.php');
         $MyConnection = new PDO('mysql:host=x;dbname=x', $dbusername, $dbpassword);
         $MyConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

         // Information from user:
         $username = htmlspecialchars($_POST['username']);
         $password = htmlspecialchars($_POST['password']);
         $lastname = htmlspecialchars($_POST['lastname']);
         $email = htmlspecialchars($_POST['email']);

         // Hashing the password:
         $cost = 10;
         $salt = strtr(base64_encode(mcrypt_create_iv(16, MCRYPT_DEV_URANDOM)), '+', '.');
         $salt = sprintf("$2a$%02d$", $cost) . $salt;
         $hash = crypt($password, $salt);

         // Check if username already exists in the database:
         $findUser = $MyConnection->prepare("SELECT Username FROM Users WHERE Username = :username OR Email = :email");
         $findUser->bindParam(':username', $username);
         $findUser->bindParam(':email', $email);
         $findUser->execute();

         $foundUser = $findUser->fetch(PDO::FETCH_OBJ);

         if($username == $foundUser->Username) {
           echo '
             <div id="pop-up">
             This username is already in use. Please choose another one.
             </div>
             ';
         }
         elseif($email == $foundUser->Email) {
           echo '
             <div id="pop-up">
             This email address is already in use. Please sign up with a different one. <br />
             If this is impossible, please <a href="contact.php">contact us</a>.
             </div>
             ';
         }
         else {
           // Store information into the database:
           $sql = $MyConnection->prepare("INSERT INTO Users (Username, Password, Lastname, Email) 
             VALUES (:username, :password, :lastname, :email");
           $sql->bindParam(':username', $username);
           $sql->bindParam(':password', $hash);
           $sql->bindParam(':lastname', $lastname);
           $sql->bindParam(':email', $email);

           if($sql->execute()) {
             echo '
               <div id="pop-up">
               Your account has succesfully been registered. You can start using it right away, by clicking
               <a href="login.php">here</a>.
               </div>
               ';
         }
       }
  }
}
?>

When I fill in information in my form, and I already use a username that I have stored into my database, I get the right pop-up, displaying that that username is already in use. But when I fill in different information (with or without the same email address) I get sent to another webpage, which doesn't exist, so my hosts takes over and displays their error screen.

Does anybody know why it sends the visitor of the webpage to another page (that doesn't exist)?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dpprx26000 2013-11-09 21:18
    关注
    $findUser = $MyConnection->prepare("SELECT Username FROM Users 
    WHERE Username = :username OR Email = :email");
    

    You are checking for:

    $email == $foundUser->Email
    

    but you never selected it from the $findUser statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题