I encrypted a directory that contains several folders by directories Admin (Protect Option), for example,memberarea
directory.
now if someone opens a page of these directories , he must enter his username and password so that he can see the contents of that directory.
I used the following code to write a welcome text to display the text related to each user. In addition, according to the entered username, an audio file will be played when the page open:
<?php
$user_name = $_SERVER['PHP_AUTH_USER'];
if ($user_name == "admin") {
$name = "Sir Alex Morgan";
echo '<div class="wellcome_message">' . $name . ' ، welcome to your site :)</div>';
echo '<script type="text/javascript">
window.onload=function(){
document.getElementById("admin_audio").play();
}
</script>';
}
elseif ($user_name == "user1") {
$name = "Miss Mary";
echo '<div class="wellcome_message">' . $name . ' ، nice to meet you</div>';
echo '<script type="text/javascript">
window.onload=function(){
document.getElementById("user1_audio").play();
}
</script>';
}
.
.
.
?>
Now I want to make the above process happen only once in each browser session, ie if the user, for example, went from page domain.com/memberarea/part1/index.php
to page domain.com/memberarea/part3/page.php
just as he does not need to enter the password again until the browser is closed, The welcome message and voice will only be displayed for the first time.
My Answer:
To achieve this, you can use a session cookie to keep track of whether the welcome message and audio have already been played in the current session. Here's an updated version of your code that includes the session cookie check:
php
$user_name = $_SERVER['PHP_AUTH_USER'];
if (!isset($_SESSION['welcome_played'])) {
if ($user_name == "admin") {
$name = "Sir Alex Morgan";
echo ' ';
echo '';
} elseif ($user_name == "user1") {
$name = "Miss Mary";
echo ' ';
echo '';
}
$_SESSION['welcome_played'] = true;
}
?>
Make sure to start the session at the beginning of your PHP script by adding `session_start();` before using `$_SESSION` variables.
This way, the welcome message and audio will only be displayed once per session for each user. The session cookie will keep track of whether the welcome message has already been played in the current session.
Rate this post
3 of 5 based on 7260 votesComments