2015-01-04 1 views
1

로그인 시스템이 작동하는 웹 사이트를 만들었고 사용자 테이블의 데이터베이스에 표준 또는 관리와 함께 열 이름 유형이 있습니다. 나는 제품을 편집 할 수있는 관리자 만위한 페이지를 만들었지 만 설정하는 방법에 대해 고민하고있어 'admin'만 로그인 한 사용자 대신 페이지를 볼 수 있습니다. 지금까지 내가 가진 것 중 Heres?세션을 사용하여 관리 페이지를 설정하고 sql 데이터베이스에 입력 하시겠습니까?

admin.php

<?session_start(); ?> 
<?php 
include 'login.php'; // this includes all my login form and login action 
include 'connection.php'; // this is my database connection 
$query1 = "SELECT type FROM users"; 
$result = mysqli_query($query1); 
$user = mysqli_fetch_array($result); 
$_SESSION['usertype'] = $user['usertype'];   

if($_SESSION['type'] = 'admin'){ 
//admin content here 
{ 
<?php 
if ($_SESSION['type']) = 'standard') 
{ 
echo 'you must be an admin to see this page'; 
} 
?> 

loginaction.php 당신은 사용자 유형을 확인 조건에서 변수 값을 설정하는 대신 comaprisons를 사용하지 않는

<?php 
session_start(); 
include'connection.php'; 

$email = trim($_POST["email"]); 
$password = trim($_POST["password"]); 
$password=md5($_POST["password"]); 
if (empty($email) or empty($password)) { 
header("Location: homepage.php?form=invalid"); //Redirection information 
exit; 
} 

if(!filter_var($email, FILTER_VALIDATE_EMAIL)) 
{ 
echo "E-mail is not valid"; 
header("Location: homepage.php?email=invalid"); 
exit; 
} 
$query = "SELECT * FROM users WHERE email= '$email' AND password = '$password' "; 
$result = mysqli_query($connection, $query) or exit("Error in query: $query. " .  mysqli_error()); 

if ($row = mysqli_fetch_assoc($result)) {//Then we have a successful login 
$_SESSION["authenticatedUserEmail"] = $email; 
$_SESSION['ID'] = $row['ID']; 
$_SESSION["password"] = $row['password']; 
header("Location: homepage.php"); 
} else {//Login was unsuccessful 
echo "User does not exist"; 
header("Location: login.php?user=invalid"); 
} 
?> 

답변

1

.

<? session_start(); ?> 
 
<? php 
 
include 'login.php'; // this includes all my login form and login action 
 
include 'connection.php'; // this is my database connection 
 
$query1 = "SELECT type FROM users"; 
 
$result = mysqli_query($query1); 
 
$user = mysqli_fetch_array($result); 
 
$_SESSION['usertype'] = $user['usertype']; 
 

 
if ($_SESSION['type'] == 'admin') { 
 
    //admin content here 
 
} 
 

 
if ($_SESSION['type']) == 'standard') { 
 
    echo 'you must be an admin to see this page'; 
 
} ?>

if($_SESSION['type'] ='admin'){ `should be` if($_SESSION['type'] == 'admin'){ 

등을 제대로 수행하지 않는 문을 종료 중괄호 퍼팅과 같은 코드의 다른 오류가 있습니다. 이 코드는 효과가 있지만 SQL 주입 및 프로그래밍 지식이있는 사람이라면 누구나 웹 사이트를 "찢어 버릴"위험이 있으므로 데이터를 도용하고 조작하는 것은 매우 안전하지 않은 코드입니다.

사용자의 SQL 인젝션 증거를 상당히 많이 입력하려면 mysql_real_escape_string()을 사용해야합니다.

0

여러 문제의 답을 @Vish에 의해 언급 된 문제와 함께, 너무 코드에 보인다

$result = mysqli_query($query1); 

는 첫 번째 인수로 connection link을 예상. 다시

:

당신이 user table에서 type를 가져 오기 위해 노력하고 있습니다. 하지만 usertypemysqli_fetch_array에 사용합니다. 잘못된 것 같습니다. 그리고 $_SESSION['type'] 변수는 실제로 $_SESSION['usertype']입니까?

수정 된 코드.

$query1 = "SELECT type FROM users"; 
$result = mysqli_query($connection, $query1); 
$user = mysqli_fetch_array($result); 
$_SESSION['usertype'] = $user['type'];   

if($_SESSION['usertype'] == 'admin') 
{ 

    //admin content here 
} 
elseif ($_SESSION['usertype']) == 'standard') 
{ 
    echo 'you must be an admin to see this page'; 
} 

P.S : 확실하지 않음이 문제를 해결할 수

관련 문제