[英]How to call a script every time a user logs in (or whenever a session is active)
I am trying run a script called random_post_generator.php
which should execute every time a user is logged in.我正在尝试运行一个名为
random_post_generator.php
的脚本,它应该在用户每次登录时执行。
I am using this approach as an alternative to cron.我正在使用这种方法作为 cron 的替代方案。
Here is how my session is currently created:以下是我的会话当前的创建方式:
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
}
?>
But how do I say - "if session is active, then run this script"?但是我怎么说 - “如果会话处于活动状态,则运行此脚本”?
<?php
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
include 'random_post_generator.php';
}
?>
or you can use require 'random_post_generator.php'
或者你可以使用
require 'random_post_generator.php'
If I understood correctly, you are trying to find out how to include a script of php (that is located in an outside .php file) inside your current file while using your previous code that checks if a user is logged in:如果我理解正确,您正在尝试找出如何在当前文件中包含 php 脚本(位于外部 .php 文件中),同时使用之前检查用户是否登录的代码:
<?php
$root_directory_path = $_SERVER['DOCUMENT_ROOT'];
ob_start();
session_start();
if (!isset($_SESSION["user_login"])) {
header("Location: index.php");
} else {
$username = $_SESSION["user_login"];
$pathName = $root_directory_path."myScript.php";//I am assuming here
//the script is located inside the root directory, and not in a sub
//directory
require($pathName);
}
?>
just remember that whatever php code is inside myScript.php has to have the <?php ?>
tag surrounding it.请记住,myScript.php 中的任何 php 代码都必须有
<?php ?>
标记围绕它。 Your code does not reuse the <?php ?>
tag of the "calling" file.您的代码不会重用“调用”文件的
<?php ?>
标记。
Let me know if that worked for you让我知道这是否对你有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.