我如何從另一個PHP文件中的PHP文件爲user_id檢索。我使用$_SESSION['user_id']
來做到這一點,但它不適合我。任何人都可以告訴我怎麼做,以及$_SESSION['user_id']
應該放在PHP文件中,還是有更好的方法。我想我已將它放在login.php
文件的正確位置,但不知道fitness.php.
我正在將它們用於我的Android應用程序。下面的兩個PHP文件。任何幫助將不勝感激,謝謝。如何從一個php文件得到一個用戶ID並在另一個php文件
的login.php
<?php
session_start();
$error = NULL;
include_once('connection.php');
if(isset($_POST['txtUsername']) && isset($_POST['txtPassword'])){
$username = $_POST['txtUsername'];
$password = $_POST['txtPassword'];
$query = "SELECT username, password, user_id FROM user WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($conn, $query);
if($username == $error || $password == $error) {
echo "Login Failed <br>";
}
elseif($result->num_rows > 0){
$_SESSION['user_id'] = 'user_id';
if(isset($_POST['mobile']) && $_POST['mobile'] == "android"){
echo "success";
exit;
}
echo "Login Successful";
// header("location: login.php");
}
else{
echo "Login Failed <br>";
}
}
?>
fitness.php
<?php
session_start();
$error = NULL;
include_once('connection.php');
if(isset($_POST['pulseOne']) && isset($_POST['pulseTwo']) && isset($_POST['pulseThree'])){
$pulseOne = $_POST['pulseOne'];
$pulseTwo = $_POST['pulseTwo'];
$pulseThree = $_POST['pulseThree'];
$fitnessResult = 100;
$overall = 30000;
$fitnessScore = -1;
$fitnessScore = $pulseOne + $pulseTwo + $pulseThree;
if($fitnessScore != 0){
$fitnessResult = $overall/$fitnessScore;
$fitnessResult = round($fitnessResult, 0);
}
else{
$fitnessResult = NULL;
}
// $fitnessResult = mydivide($overall/$fitnessScore);
$date = date("Y-m-d");
$time = date("h:i:sa");
// $user_id = $_POST['user_id'];
$query = "INSERT INTO `fitness`(`fitnessScore`, `fitnessDate`,`fitnessTime`, `user_id`) VALUES ('$fitnessResult','$date','$time', 42)";
$result = mysqli_query($conn, $query);
if($pulseOne == $error || $pulseTwo == $error || $pulseThree == $error){
echo "Insert Failed";
}
elseif($result > 0){
if(isset($_POST['mobile']) && $_POST['mobile'] == "android"){
echo "success";
exit;
}
echo "Insert Successfully";
}
else{
if(isset($_POST['mobile']) && $_POST['mobile'] == "android"){
echo "Registration Failed";
exit;
}
echo "Insert Failed";
}
}
?>
你的代碼是在**得到的SQL注入的**危險。參見[如何-可以-I-防止-SQL注入功能於PHP(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1 ) –
像SQL注入漏洞這樣的錯誤很多。採用明文存儲密碼等你 – e4c5
有回聲$ _SESSION [「user_ID的」]無論你想要的user_id –