[英]PHP fetch(get) specific data from specific user when logged in
However my problem is that I want the logged in person to see "for example [age,wallet]etc.. 但是我的问题是我希望登录的人看到“例如[年龄,钱包]等。
I used the login and register script from a website to get me started. 我使用网站上的登录和注册脚本来入门。 They already had the code for name ( When someone logged in it said, Hello, ['username'] )
他们已经有了名称代码(当有人登录时说“你好,['用户名']”)
The url for the tutorial is here: How to Create a Secure Login Script in PHP and MySQL 本教程的网址在这里: 如何在PHP和MySQL中创建安全登录脚本
It might not be the safest or best way to create but that is why I am learning. 这可能不是最安全或最佳的创建方法,但这就是我正在学习的原因。
EDIT: Just to make my question more understandable: I wan't to get data from my database like age for example. 编辑:只是为了使我的问题更容易理解:例如,我将不会从数据库中获取数据,例如age。 When I log in I get Hello, Chris.
登录后,您好,克里斯。 But I want Hello, Chris.
但是我想问你好,克里斯。 Your age is 20 for example.
例如,您的年龄是20岁。 I can't seem to get this done no matter what I try.
无论我如何尝试,我似乎都无法完成此任务。 Code:
码:
index.php index.php
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Project 0N3</title>
<link rel="stylesheet" href="style/style.css" />
<meta name="generator" content="localhost">
</head>
<body>
<div id="wrapper">
<div id="top-panel-right-beta">
<a class="two" href="index.php">Dark Theme | </a>
<a class="two" href="light-index.php">Light Theme[BETA]</a>
</div>
<?php if(login_check($mysqli) == true) : ?>
<div class="welcome">
<p>Hello, <?php echo htmlentities ($_SESSION['username']); ?>!</p>
</div>
<div id="waller">
<?php
?>
<p>Your Wallet: <?php echo ($_SESSION['wallet']);?></p>
</div>
<div id="home">
<div id="top-panel">
<a class="one" href="index.php" >Home | </a>
<a class="one" href="membersarea.php">Members Area | </a>
<a class="one" href="createaccount.php"> Create a game account | </a>
<a class="one" href="includes/logout.php">Log Out</a>
</div>
<div id="content">
<h1>It is all about Project 0N3</h1>
<p class="alpha">Alpha 0.0.1</p>
<p>Mauris convallis ipsum lorem, id posuere sem semper vitae. Curabitur elementum, nibh a lobortis eleifend, sem diam ultricies massa, non congue risus dui vitae purus. Aliquam ut lectus vitae quam hendrerit volutpat. Sed iaculis vestibulum sapien. Aenean molestie, tortor faucibus condimentum consequat, quam risus ultrices orci, sed sagittis velit magna ultrices purus. Phasellus non odio eget erat pellentesque ullamcorper eget sit amet libero. Cras vel facilisis ipsum. Quisque rutrum viverra consequat. Sed posuere eros in nisi lobortis accumsan quis ac neque. Aliquam at diam rhoncus, accumsan nunc id, fringilla elit. Integer scelerisque leo nec metus accumsan mollis. Curabitur iaculis nibh et turpis gravida pulvinar.
</p>
<div id="footer">
© 2014 Project 0N3. All rights reserved
</div>
</div>
<?php else : ?>
<div id="top-panel-right_2">
<a class="one" href="registeraccount.php">Register | </a>
<a class="one" href="login.php">Log in</a>
</div>
<div id="content-noacc">
<h1 class="h1beta"> ALPHA Ver 0.0.1 </h1>
<div class="logohuge">
<a href="about.php"><img src="images/logo01.png" width="" height="" alt="logo01"></a>
</div>
</div>
<div id="footer2">
© 2014 Project 0N3. All rights reserved
</div>
<?php endif; ?>
</div>
<!-- End of div wrapper -->
</div>
</body>
</html>
function.php function.php
<?php
include_once 'psl-config.php';
function sec_session_start()
{
$session_name = 'sec_session_id'; // Set a custom session name
$secure = 'SECURE';
// This stops JavaScript being able to access the session id.
$httponly = true;
// Forces sessions to only use cookies.
if (ini_set('session.use_only_cookies', 1) === FALSE) {
header("Location: ../error.php?err=Could not initiate a safe session (ini_set)");
exit();
}
// Gets current cookies params.
$cookieParams = session_get_cookie_params();
session_set_cookie_params($cookieParams["lifetime"],
$cookieParams["path"],
$cookieParams["domain"],
$secure,
$httponly);
// Sets the session name to the one set above.
session_name($session_name);
session_start(); // Start the PHP session
session_regenerate_id(); // regenerated the session, delete the old one.
}
function login($email, $password, $mysqli, $wallet) {
// Using prepared statements means that SQL injection is not possible.
if ($stmt = $mysqli->prepare("SELECT id, username, password, salt, wallet
FROM members
WHERE email = ?
LIMIT 1")) {
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($user_id, $username, $db_password, $salt, $wallet);
$stmt->fetch();
// hash the password with the unique salt.
$password = hash('sha512', $password . $salt);
if ($stmt->num_rows == 1) {
// If the user exists we check if the account is locked
// from too many login attempts
if (checkbrute($user_id, $mysqli) == true) {
// Account is locked
// Send an email to user saying their account is locked
return false;
} else {
// Check if the password in the database matches
// the password the user submitted.
if ($db_password == $password) {
// Password is correct!
// Get the user-agent string of the user.
$user_browser = $_SERVER['HTTP_USER_AGENT'];
// XSS protection as we might print this value
$user_id = preg_replace("/[^0-9]+/", "", $user_id);
$_SESSION['user_id'] = $user_id;
// XSS protection as we might print this value
$username = preg_replace("/[^a-zA-Z0-9_\-]+/",
"",
$username);
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512',
$password . $user_browser);
// Login successful.
return true;
} else {
// Password is not correct
// We record this attempt in the database
$now = time();
$mysqli->query("INSERT INTO login_attempts(user_id, time)
VALUES ('$user_id', '$now')");
return false;
}
}
} else {
// No user exists.
return false;
}
}
}
function checkbrute($user_id, $mysqli) {
// Get timestamp of current time
$now = time();
// All login attempts are counted from the past 2 hours.
$valid_attempts = $now - (2 * 60 * 60);
if ($stmt = $mysqli->prepare("SELECT time
FROM login_attempts <code><pre>
WHERE user_id = ?
AND time > '$valid_attempts'")) {
$stmt->bind_param('i', $user_id);
// Execute the prepared query.
$stmt->execute();
$stmt->store_result();
// If there have been more than 5 failed logins
if ($stmt->num_rows > 5) {
return true;
} else {
return false;
}
}
}
function login_check($mysqli) {
// Check if all session variables are set
if (isset($_SESSION['user_id'],
$_SESSION['username'],
$_SESSION['login_string'])) {
$user_id = $_SESSION['user_id'];
$login_string = $_SESSION['login_string'];
$username = $_SESSION['username'];
// Get the user-agent string of the user.
$user_browser = $_SERVER['HTTP_USER_AGENT'];
if ($stmt = $mysqli->prepare("SELECT password
FROM members
WHERE id = ? LIMIT 1")) {
// Bind "$user_id" to parameter.
$stmt->bind_param('i', $user_id);
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
if ($stmt->num_rows == 1) {
// If the user exists get variables from result.
$stmt->bind_result($password);
$stmt->fetch();
$login_check = hash('sha512', $password . $user_browser);
if ($login_check == $login_string) {
// Logged In!!!!
return true;
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
} else {
// Not logged in
return false;
}
}
function esc_url($url) {
if ('' == $url) {
return $url;
}
$url = preg_replace('|[^a-z0-9-~+_.?#=!&;,/:%@$\|*\'()\\x80-\\xff]|i', '', $url);
$strip = array('%0d', '%0a', '%0D', '%0A');
$url = (string) $url;
$count = 1;
while ($count) {
$url = str_replace($strip, '', $url, $count);
}
$url = str_replace(';//', '://', $url);
$url = htmlentities($url);
$url = str_replace('&', '&', $url);
$url = str_replace("'", ''', $url);
if ($url[0] !== '/') {
// We're only interested in relative links from $_SERVER['PHP_SELF']
return '';
} else {
return $url;
}
}
Assuming that age
is a column in your members
table, you'll need to make a few changes… 假设“
age
是您的“ members
表中的一列,则需要进行一些更改……
// add age to the SELECT query
if ($stmt = $mysqli->prepare("SELECT id, username, password, salt, wallet, age
FROM members…
// add $age to the bind_result call
$stmt->bind_result($user_id, $username, $db_password, $salt, $wallet);
$stmt->fetch();
…
// add $age to the SESSION
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512',
$password . $user_browser);
$_SESSION['age'] = $age;
…
// Access the SESSION value
<div class="welcome">
<p>Hello, <?php echo htmlentities ($_SESSION['username']); ?>. Your age is <?php echo htmlentities ($_SESSION['age']); ?>.</p>
</div>
That should at least point you in the right direction. 那至少应该为您指明正确的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.