PHP login snippet

Published
2022-07-07
浏览次数 :  267

 //check the database if the username is taken
    $sql = "SELECT username FROM users WHERE username=?";
    $stmt = $conn->prepare($sql);
    $stmt->execute([$userName]);
    if ($stmt->rowCount() > 0) {
      $em = "The username ($username) is taken!";
      header("Location:../../sign.php?error=$em&$data");
      exit;
    } else {
      if (isset($_FILES['profile'])) {
        //WHEN USE ISSET $_FILES THERR IS ALWAY ERROR 4 
        // get data and store them in var
        $imgName = $_FILES['profile']['name'];
        $tmp_name = $_FILES['profile']['tmp_name'];
        $error = $_FILES['profile']['error'];

        //if there is no error occured while uploading
        if ($error === 0) {
          // get image extension store it in var 
          $img_ex = pathinfo($imgName,PATHINFO_EXTENSION);

          // CONVERT IMAGE EXTENSION INTO LOWER CASE AND STORE IT IN VAR 
          $img_ex_lc = strtolower($img_ex);

          //Creating array that stores allowed to upload image extension
          $allowed_exs = array("jpg","jpeg","png");

          // check if the image extension is in allowed extension array
          
          if (in_array($img_ex_lc, $allowed_exs)) {
            // rename the image with username 
            $new_img_name = $userName . '.' .$img_ex_lc;
            //create upload path on root
            //$img_upload_path = '../../uploads'.$new_img_name;
            $img_upload_path = '../../uploads/' . $new_img_name;
            //move uploaded image to /upload folder
            move_uploaded_file($tmp_name, $img_upload_path);

          } else {
            $em = "You can't upload that type of file!";
            header("Location:../../sign.php?error=$em&$data");
            exit;
          }
        } 

  • 标签1
  • 标签1
  • 标签1
Top