我有一个用户可以登录的索引页面,他们被重定向到 dashboard.php,我已经开始了一个 session
require_once("config.php");
if (!isset($_SESSION["user_id"]) || $_SESSION["user_id"] == "") {
// not logged in send to login page
redirect("index.php");
我想显示一条每日消息(每个成员每天只显示一次) 我还编码为仅从特定 ip 可见。
这就是我所拥有的,但它不起作用...它使用 cookie,我相信如果我需要为每个成员发送每日消息,我还需要其他东西。
<div id="test">
<?php
$allow = array("84.232.237.169","188.24.146.118"); //allowed IPs
if(!in_array($_SERVER['REMOTE_ADDR'], $allow) && !in_array($_SERVER["HTTP_X_FORWARDED_FOR"], $allow) && !in_array($_SERVER["HTTP_CLIENT_IP"], $allow)) { ?>
<?php } else { ?>
<script>
$(document).ready(function() {
$('#welcome-modal').modal('show');
setTimeout(function() {
$('#welcome-modal').modal('hide');
}, 60000);
});
</script>
<div id="user">
<div id="welcome-modal" class="modal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body" align="center">
<p>
<font size="6">
<div align="center"></div>Salut, Nu uita sa te pontezi! <br><br>
Click <a href="pontajianuarie.php">AICI</a> pentru a te ponta!
</div>
</font>
</p>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
<input type="button" value="Delete Cookie" onclick="deleteCookie()"/>
<script>
// 'Delete' cookie for testing
function deleteCookie()
{
if(typeof(Storage) !== "undefined"){
sessionStorage.returnVisit = "false";
console.log("sessionStorage set.");
}
else{
setCookie("return_visit","false",1);
}
}
//W3 Schools setCookie function
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}
//W3 Schools getCookie function
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
//Check return_visit cookie on page load
function bodyOnload()
{
if(getCookie("return_visit") === 'true' || sessionStorage.returnVisit === "true"){
//Do something if user has already visited page
var test = document.getElementById("test");
test.innerHTML = "";
}
else
{
if(typeof(Storage) !== "undefined"){
sessionStorage.returnVisit = "true";
console.log('Session Storage set.');
}
else{
setCookie("return_visit","true",1);
}
}
}
</script>
编辑:
<?php
require_once("config.php");
if (!isset($_SESSION["user_id"]) || $_SESSION["user_id"] == "") {
// not logged in send to login page
redirect("index.php");
}
?>
<?php
$servername = "localhost";
$username = "highmob";
$password = "pass";
$dbname = "highmob_comenzi";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function show_message($user_id) {
$sql = "SELECT * FROM DAILY_MESSAGES WHERE user_id = ".$_SESSION['user_id'] . "";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row['date'] == date('Y-m-d')) {
if($row['used']==0) {
echo $row['message'];
}
}
}
}
}
function update_message_status($user_id) {
$sql = 'UPDATE DAILY_MESSAGES SET used=1 WHERE '.$_SESSION['user_id'] . "";
$conn->query($sql);
}
?>
我还创建了名为 DAILY_MESSAGES 的数据库表 ID = AI消息, 用户身份, 用过的, 创建于
我没有收到任何消息或任何数据库更新?
最佳答案
您可以使用数据库,例如,如果您使用的是 mysql,则可以创建一个具有以下结构的表 DAILY_MESSAGES:
ID: int AI
message: string
user_id: int
used: int
created_at: date
假设如果用户进入您的仪表板,您将打印消息并更新使用的字段,例如
function show_message($user_id) {
$sql = 'SELECT * FROM daily_messages WHERE user_id='.$user_id;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row['date'] == date('Y-m-d')) {
if($row['used']==0) {
echo $row['message'];
}
}
}
}
}
和更新
function update_message_status($user_id) {
$sql = 'UPDATE daily_messages SET used=1 WHERE '.$user_id;
$conn->query($sql)
}
关于javascript - 每个用户登录后显示每日消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55406381/