2013-10-08 2 views
2

로그인 한 세션을 사용하여 누가 양식을 제출했는지 알고 싶습니다. 일단 로그인 한 USERS 테이블에서 'user_id'를 검색하려고합니다. 그런 다음 리뷰를 작성하고 양식을 제출하면 user_id가 필름 테이블로 전송됩니다.세션 데이터를 사용하여 누가 양식을 제출했는지 알아보십시오.

도움이 될 것입니다. 고맙습니다. 여기

-- Table structure for table `films` 
-- 

CREATE TABLE IF NOT EXISTS `films` (
    `movie_id` int(4) NOT NULL AUTO_INCREMENT, 
    `movie_title` varchar(100) NOT NULL, 
    `actor` varchar(100) NOT NULL, 
    `rating` varchar(20) NOT NULL, 
    `user_id` int(100) NOT NULL, 
    PRIMARY KEY (`movie_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=38 ; 

-- 
-- Table structure for table `users` 
-- 

CREATE TABLE IF NOT EXISTS `users` (
    `user_id` int(4) NOT NULL AUTO_INCREMENT, 
    `email` varchar(40) NOT NULL, 
    `password` varchar(40) NOT NULL, 
    `name` varchar(30) NOT NULL, 
    PRIMARY KEY (`user_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ; 


INSERT INTO `users` (`user_id`, `email`, `password`, `name`) VALUES 
(1, '[email protected]', 'password', 'Ben'); 

-- 
-- Constraints for table `reviewed` 
-- 
ALTER TABLE `reviewed` 
    ADD CONSTRAINT `reviewed_ibfk_1` FOREIGN KEY (`movie_id`) REFERENCES `films` (`movie_id`), 
    ADD CONSTRAINT `reviewed_ibfk_2` FOREIGN KEY (`movie_id`) REFERENCES `films` (`movie_id`) ON DELETE CASCADE; 

세션이 내가 제대로 작성하지하고 있으리라 믿고있어 생성 된 로그인 양식입니다.

<?php 
    include('./includes/header.php'); 

    if (isset($_POST['submit'])) { 
     $error = array(); // Initialize error array. 

     // Check for a email. 
     if (empty($_POST['email'])) { 
      $error[] = "Please neter a email"; 
     } else { 
      $email = $_POST['email']; 
     } 

     // Check for a password. 
     if (empty($_POST['password'])) { 
      $error[] = "Please enter a password"; 
     } else { 
      $password = $_POST['password']; 
     } 

     if (empty($error)) { // No errors found 
      require_once('./includes/mysql_connect.php'); 
      $match = "SELECT * FROM users WHERE email='$email' AND password='$password'"; 
      $qry = mysql_query($match); 
      $num_rows = mysql_num_rows($qry); 

      if ($num_rows == true) { 
       $_SESSION['user_id']=$_POST['email']; 
       header("location:index.php"); 
      } else { 
       echo "No user name or id "; 
      } 
     } else { 
      foreach ($error as $msg) { 
       echo $msg; 
      } 
     } 
    } 
?> 

<html> 
<form method="post" action="login.php"> 
    <fieldset><legend>Login</legend> 
     <label for="email">Email</label> 
     <input type="text" name="email" id="email" /> 
     <br/> 
     <label for="password">Password</label> 
     <input type="password" name="password" id="password" /> 
     <br/> 
    <input type="submit" name="submit" value="login" /> 
    </fieldset> 
</form> 
</html> 


<?php 
    include('./includes/footer.php'); 
?> 

내가

<?php 
    include('./includes/header.php'); 
    echo "<h1>Add A film</h1>"; 
     if(isset($_POST['submitted'])){ 
     $errors = array(); // Initialize error array. 
     $user = $_SESSION['user_id']; 

     // Check for title. 
     if (empty($_POST['movie_title'])){ 
      $errors[] = "You forgot to enter a title."; 
     } else { 
      $mt = (trim($_POST['movie_title'])); 
     } 
     // Check for leading actor 
     if (empty($_POST['leading_actor'])){ 
      $errors[] = "You forgot to enter a actor"; 
     } else { 
      $la = (trim($_POST['leading_actor'])); 
     } 
     // Check for a rating 
     if (empty($_POST['rating'])){ 
      $errors[] = "Please select a rating."; 
     } else { 
      $rating = ($_POST['rating']); 
     } 
     // Check for a review 
     if (empty($_POST['review'])){ 
      $errors[] = "Please write a review"; 
     } else { 
      $review = (trim($_POST['review'])); 
     } 
     if (empty($errors)) { // If no errors were found. 
      require_once('./includes/mysql_connect.php'); 

      // Make the insert query. 
      $query = "INSERT INTO films (movie_title, actor, rating, user_id) 
      Values ('$mt', '$la', '$rating', '$user')"; 
      $result = mysql_query($query); 
      $id = mysql_insert_id(); 
      $query = "INSERT INTO reviewed (review, movie_id) 
      values ('$review', '$id')"; 
      $result = mysql_query($query); 

      //Report errors. 
     } else { 
      foreach ($errors as $msg){ 
       echo " - $msg <br/> "; 
      } 
     } 
    }; 
?> 

<html> 
<form action="review_a_film.php" method="post" id="review_a_film"> 
    <fieldset> 
     <label for="title">Movie Title</label> 
     <input type="text" name="movie_title" id="movie_title" /> 
     <br/> 
     <br/> 
     <label for="actor">Leading Actor</label> 
     <input type="text" name="leading_actor" id="leading_name" /> 
     <br/> 
     <br/> 
     <label for="rating">Rating</label> 
     <select id="rating" name="rating"/> 
      <option selected="selected" value=0 disabled="disabled">Select a Rating</option> 
      <option value="Terrible">Terrible</option> 
      <option value="Fair">Fair</option> 
      <option value="Ok">Ok</option> 
      <option value="Good">Good</option> 
      <option value="Excellent">Excellent</option> 
     </select> 
     <br/> 
     <br/> 
     <label for="review">Your Review</label> 
     <br/> 
     <textarea name="review" id="review" rows="15" cols="60"></textarea> 
     <br/> 
     <br/> 
     <input type="submit" name="submit" id="submit" value="submit" /> 
     <input type="hidden" name="submitted" value="TRUE" /> 
    </fieldset> 
</form> 
</html> 

<?php 
    include('./includes/footer.php'); 
?> 
+1

전자 메일/암호 자격 증명을 확인하고 있습니다. 자격 증명이 일치하면 이미 사용자가 있습니다. 이제'user_id'를 가져 와서'$ _SESSION [ 'user_id']'에 저장하십시오. 사용자가 영화 리뷰를 제출할 때'$ _SESSION [ 'user_id']'에서 user_id를 검색하고 SQL INSERT 문으로 전달하십시오. – WebNovice

+1

[** 정말로 암호를 일반 텍스트로 저장하고 있습니까? **] (http://plaintextoffenders.com/)이 사용자 시스템을 사용하고 있지 않아야합니다. 적어도 지금은 아니야. 보안에 대해 배우는 ** 시간 **을 보내고 시도해보십시오. 웹의 보안뿐만 아니라 사용자의 보안을 위험에 빠뜨리는 것처럼 시도하십시오. 자세한 내용은 보안 해시 알고리즘을 사용하여 해시해야합니다 (http://stackoverflow.com/q/401656/938236) (http://stackoverflow.com/q/4795385/938236). 또한, ** 명백한 SQL 주입 취약점 **이 있습니다. –

답변

2

귀하의 질문에 대답하기 위해 MySQL 데이터베이스에 세션 USER_ID를 전송하고자하는 리뷰 양식 : 변수 $ _SESSION [ 'USER_ID에서 스토어 자신의 USER_ID를 ']를 선택하고 로그 아웃 할 때 $ _SESSION ['user_id ']을 지우십시오.

그러나 수정해야 할 다른 문제가 있습니다.

암호를 일반 텍스트로 저장할 수 없습니다. 이것은 좋은 습관이 아닙니다. 해킹당한 경우 (SQL 취약점으로 인한 것으로 보이고 잠시 후에) 암호를 일반 텍스트로 저장하면 사람들이 너에게 화를 낼 것입니다. 암호 해싱을 조사해야합니다. 다음은 시작하기위한 몇 가지 링크입니다. Encryption using mcrypt, PHP, and MySQL How can I store my users' passwords safely?

또한 SQL 주입에 취약합니다. 매개 변수화 된 쿼리를 사용하거나 사용자가 코드에 SQL을 삽입 할 수 있어야합니다. 이 같은 PHP를 봐

Paramaterized 쿼리 :

$var = $unsafevar; 
$stmt = mysqli_prepare($connection, "SELECT * FROM users WHERE username = ?"); 
mysqli_stmt_bind_param($stmt, 's', $var); 
mysqli_stmt_execute($stmt); 
$result = mysqli_stmt_get_result($stmt); 
$row = mysqli_fetch_assoc($result); 

의는 바인더 제본 및 bind_param 삽입되는 변수를 나타낼?.

체크 아웃 OWASP의 SQL 주입 페이지와 자신의 최고 열 : 당신도 온라인 사용자 데이터베이스와 사이트에 게시 고려하기 전에

Owasp SQL injection

Owasp top ten

당신은이 일을 배워야한다.

관련 문제