2016-11-27 2 views
0

로그인 한 사용자의 사용자 ID를 가져 와서 세션에 저장하려고합니다. 사용자가 무언가를 제출하면 사용자 ID를 제출자에게 추가 할 수 있습니다. 데이터 베이스. 나는 매우 거친 로그인 페이지를 가지고있어, 친절하게 대해주십시오. 여러 가지 다양한 조합을 시도했지만 온라인에서 보았던 답변으로 이동했습니다. 현재 가지고있는 답변은 매우 비슷합니다. 누구든지 내게 전혀 포인터를 줄 수 있다면 큰 도움이 될 것입니다. 고맙습니다!세션에 사용자 ID를 저장할 수 없습니다.

로그인 페이지

<?php 

session_start(); 

//Connection and select database go in here 

// username and password sent from form 
$myusername=$_POST['Email']; 
$mypassword=$_POST['User_Password']; 

// To protect MySQL injection 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql="SELECT User_ID, Email, User_Password FROM $tbl_name WHERE Email='$myusername' and User_Password='$mypassword'"; 
$result=mysql_query($sql); 

// Mysql_num_row counts table row 
$count=mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count==1){ 

// is_auth to make sure they can view other pages that need credentials. 
$_SESSION['is_auth'] = true; 
$_SESSION['User_ID'] = $result->User_ID; 

// Once the sessions variables have been set, redirect them to the landing page/home page. 
header('location: ../View/main.php'); 
exit; 
} 

else { 
$error = "Please enter an email and password to login."; 
} 
header("location:../View/mainUnauthenticated.php"); 

사용자가 인증 된 경우 확인 페이지. 나는 이것이 내가 (내가 가진 생각!) 저장된 사용자 ID를 잡아 수 있도록하고 싶습니다 페이지 중 하나에서 조각있는 모든 관련 페이지

<?php 

    session_start(); 
echo $_SESSION['User_ID']; 

// Test the session to see if is_auth flag was set (meaning they logged in successfully) 

// If test fails, send the user to homepage and prevent rest of page being shown. 

if (!isset($_SESSION["is_auth"])) { 
header("location: ../View/mainUnauthenticated.php"); 
exit; 
    } 
else if (isset($_REQUEST['logout']) && $_REQUEST['logout'] == "true") { 
// At any time we can logout by sending a "logout" value which will unset the "is_auth" flag. 
// We can also destroy the session if so desired. 
unset($_SESSION['is_auth']); 
session_destroy(); 
// After logout, send them back to homepage 
header("location: ../View/mainUnauthenticated.php"); 
exit; 
} 
?> 

의 시작이를 호출합니다. 지금은 텍스트 필드에 표시되도록 노력하고 있습니다.

<?php include('../Controller/is_auth.php') 

?> 


<p> 
<input type="text" name="User_ID" id="User_ID" value="<?php echo $_SESSION['User_ID'];?>"/> 
</p> 
+1

mysql을 사용하지 말고 대신 mysqli 나 PDO를 사용하면 mysql injection을 할 수있다. – Nytrix

+0

이제는 제대로 작동한다고 생각합니다. 문제는 내 세션 시작의 위치 였고, 로그인 페이지의 맨 위에 놓았지만 SQL 쿼리 이후로 옮겨서 작동했습니다! 도와 주셔서 감사하고 Nytrix 및 Filip Macek에게 의견을 전합니다. –

답변

0

우선, 이전 mysql_query 대신 PDO을 사용하실 것을 권해드립니다. 나는 SQL injection보다 안전합니다.

오류 : 변수의 출력을 보려면 간단한 "디버그"기능을 만드는 것이 좋습니다. 예를 들어 로그인 스크립트에서 var $ result-> User_ID를 사용합니다. 그 변수가 확실합니까? 당신은 당신이 제외되는 것을 얻을하고 문제를 발견하면 간단히 확인할 수있는이 기능으로

function debug ($var, $name = "Debug") { 
    echo "<h2>$name</h2><pre>"; 
    var_dump($var); 
    echo "</pre>"; 
} 

: 여기 당신이 사용할 수있는 내 간단한 디버그 기능입니다.

debug($result, "Database output");//or without 2nd parameter 

추 신 : 모든 코드에 포함시킬 functions.php를 만듭니다. 일부 기능을 변경하면 모든 파일을 다시 쓸 필요가 없습니다.

관련 문제