2013-06-17 6 views
0

저는 PHP를 처음 사용하고 회원 로그인이없는 웹 사이트를 만드려고합니다 (회원 패널 없음). 로그인 한 경우에만 일부 페이지를 볼 수있는 간단한 웹 페이지가 있습니다.PHP/MYSQL _SESSION에 이름을 부여하는 방법

_SESSION에 임시 이름을 지정하는 방법을 알려주시겠습니까?

는 로그인 스크립트입니다

if (isset($_POST['formsubmitted'])) { 
// Initialize a session: 
session_start(); 
$error = array();//this aaray will store all error messages 


if (empty($_POST['e-mail'])) {//if the email supplied is empty 
    $error[] = 'You forgot to enter your Email '; 
} else { 


    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) { 

     $Email = $_POST['e-mail']; 
    } else { 
     $error[] = 'Your EMail Address is invalid '; 
    } 


} 


if (empty($_POST['Password'])) { 
    $error[] = 'Please Enter Your Password '; 
} else { 
    $Password = $_POST['Password']; 
} 


    if (empty($error))//if the array is empty , it means no error found 
{ 



    $query_check_credentials = "SELECT * FROM members WHERE (Email='$Email' AND password='$Password' AND Aprobat='DA') AND Activation IS NULL"; 



    $result_check_credentials = mysqli_query($dbc, $query_check_credentials); 
    if(!$result_check_credentials){//If the Query Failed 
     echo 'Query Failed '; 
    } 

    if (@mysqli_num_rows($result_check_credentials) == 1)//if Query is successful 
    { // A match was made. 




     $_SESSION = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);//Assign the result of this query to SESSION Global Variable 
     $_SESSION['start'] = time(); // taking now logged in time 
     $_SESSION['expire'] = $_SESSION['start'] + (30 * 60) ; 
     header("Location: about.php"); 


    }else 
    { 

     $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect'; 
    } 

} else { 



echo '<div class="errormsgbox"> <ol>'; 
    foreach ($error as $key => $values) { 

     echo ' <li>'.$values.'</li>'; 



    } 
    echo '</ol></div>'; 

} 


if(isset($msg_error)){ 

    echo '<div class="warning">'.$msg_error.' </div>'; 
} 
/// var_dump($error); 
mysqli_close($dbc); 

} 

이는 로그인 한 사용자가 볼 수있는 페이지의 스크립트입니다.

session_start(); 
unset($_SESSION["username"]); // where $_SESSION["nome"] is your own variable. if you do not have one use only this as follow **session_unset();** 
session_destroy(); 
header("Location: index.php"); 

누군가가이 스크립트를 개선하는 데 도움한다면 정말 뵙죠 것 :

ob_start(); 
session_start(); 
if(!isset($_SESSION['Username'])){ 
    header("Location: login.php"); 
session_destroy(); 
} 

는 이는 로그 아웃 스크립트입니다. 내가 원하는 또 다른 것은 _SESSION이 올바르게 만료되었는지 여부입니다.

l m trying to use Prepared statement and I don 사용 방법을 알고 있어야합니다.

은 준비된 문을 사용하여 코드이며,이 오류를받을 : 치명적인 오류 : 라인에 /home/siteseby/public_html/login.php에서 비 객체() 멤버 함수의 bind_param에 전화 (38)

if (isset($_POST['formsubmitted'])) { 
// Initialize a session: 
session_start(); 
$error = array();//this aaray will store all error messages 


if (empty($_POST['e-mail'])) {//if the email supplied is empty 
    $error[] = 'You forgot to enter your Email '; 
} else { 


    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST['e-mail'])) { 

     $Email = $_POST['e-mail']; 
    } else { 
     $error[] = 'Your EMail Address is invalid '; 
    } 


} 


if (empty($_POST['Password'])) { 
    $error[] = 'Please Enter Your Password '; 
} else { 
    $Password = $_POST['Password']; 
} 


    if (empty($error))//if the array is empty , it means no error found 
{ 

$qstmt = "SELECT * FROM users WHERE (Email = ? AND password = ? AND Aprobat='DA') AND Activation IS NULL"; 
$stmt = $dbc->prepare($qstmt); 
$stmt->bind_param($Email, $Password); 
$query_check_credentials = $stmt->execute(); 
$result_check_credentials = $query_check_credentials; 
    if(!$result_check_credentials){//If the QUery Failed 
     echo 'Query Failed '; 
    } 

    if (@mysqli_num_rows($result_check_credentials) == 1)//if Query is successfull 
    { // A match was made. 




     $_SESSION = mysqli_fetch_array($result_check_credentials, MYSQLI_ASSOC);//Assign the result of this query to SESSION Global Variable 
     $_SESSION['start'] = time(); // taking now logged in time 
     $_SESSION['expire'] = $_SESSION['start'] + (2 * 60) ; 
     $_SESSION['email'] = $Email; 
     header("Location: about.php"); 
     exit; 


    }else 
    { 

     $msg_error= 'Either Your Account is inactive or Email address /Password is Incorrect'; 
    } 

} else { 



echo '<div class="errormsgbox"> <ol>'; 
    foreach ($error as $key => $values) { 

     echo ' <li>'.$values.'</li>'; 



    } 
    echo '</ol></div>'; 

} 


if(isset($msg_error)){ 

    echo '<div class="warning">'.$msg_error.' </div>'; 
} 
/// var_dump($error); 
mysqli_close($dbc); 

} // End of the main Submit conditional. 

답변

0

다, I에 유래 여기 세션 하이재킹/고정 관련 Q/A를 확인하도록 조언을 것입니다 지금까지 당신이 세션을 처리하는 방법으로 (에서 - 적어도, 초보자를위한)

... 잘 보인다 . 또는 SSL을 사용하여 데이터를 암호화하는 것이 더 좋습니다. 그 말은, 당신이 더 나은 생각, 준비된 진술을 사용하여 MySQL 쿼리를 처리하고, 어색한 방법 대신 명명 된 매개 변수 (Email='$Email' AND password='$Password 다시, 당신은 여기에서 쉽게 튜토리얼을 찾을 수 있습니다.

코드의 올바른 형식을 지정하고 if (@mysqli_과 같은 오류 억압을 피하고 header("Location: login.php");을 사용하여 머리말을 리디렉션 한 후에 exit를 사용하지 않는 것은 일반적으로 좋지 않은 아이디어로 간주됩니다.

+0

예. –

+0

내가 이해하면 모르겠다. 그것은 바로이 준비 문 : $ query_check_credentials = $ dbh-> prepare ("SELECT * FROM users WHERE (Email =? AND password =?\t $ query_check_credentials-> execute (array ($ Email, $ Password)); $ query_check_credentials = "WHERE (Email ="DA ") 및 활성화는 NULL입니다. '$ Email'AND password = '$ Password'AND Aprobat = 'DA') 활성화 IS NULL " \t $ result_check_credentials = mysqli_query ($ dbc, $ query_check_credentials); –

0

먼저 세션 작성과 관련된 쿠키가 HTTP 헤더로 전송되기 때문에 코드 시작 부분에 session_start()으로 전화해야합니다. 다음 것은, 로그인 스크립트의 끝에서, 따라서 세션 설정 :

지금 세션이 설정되어 있는지
$_SESSION['email'] = $email; 

, 당신은 사용자가 볼 로그인 할 필요가 각 페이지에서 확인해야합니다을 (기억 난 당신이 먼저 '사용자 이름'세션을 설정하지 않고 if(!isset($_SESSION['Username']))를 통과보기 때문에

가 이전에 설정되어있는 경우는
session_start(); 
if (isset($_SESSION['email'] { 

//whatever should happen with a logged-in user 
} 

당신은 단지 사용자 세션을 사용할 수있는 기억, 내가 이런 말을 해요 : 무엇이든)을 출력하기 전에 session_start()로 시작하는 .

+0

고맙습니다. 몇 가지 준비된 문장을 시도하고 나는 그것을 사용하는 방법을 모르겠다. 나는 짧은 검증 후 몇 분 안에 여기에 게시하고 괜찮은지 말해. 안부 인사 –

+0

@ user2476097 당신이 원래의 질문을 완전히 다른 무언가로 변경하는 대신 ,이 답을 올바른 것으로 받아 들여서 새로운 문제가 생길 경우 새로운 질문을 올리십시오. – 1615903