I am using the session to show some text. The result I want is After 10s of logging it should echo some text.
I tried:-
Functions.php
function LoginSessionExpired() {
if(!isset($_SESSION['first_visit']))
{
$_SESSION['first_visit']=time();
}else{
$login_session_durations = 10;
if(isset($_SESSION['loggedin_time']) and isset($_SESSION["user_id"])){
if(((time() - $_SESSION['first_visit']) > $login_session_durations)){
return true;
}
}
}
}
Dashboard.php
session_start();
include("functions.php");
if(isset($_SESSION["user_id"])) {
if(LoginSessionExpired()) {
echo "Ok";
}
}
But it echoes only when the page is refreshed, I want the auto show without refreshing.
Related
function that runs when i want to edit a client status.
// deletes (sets inactive) client
function setClientStatus($client) {
if ($_GET['action'] == "setinactive") {
//database functions
$sql = 'UPDATE clients SET active="0" WHERE id="'. $client .'"';
$result = query($sql);
confirmQuery($result);
//set message to user to display
$_SESSION['error'] = false;
$msgs[] = "Client was set to inactive and removed from the list!";
$_SESSION['userMsg'] = $msgs;
}
if ($_GET['action'] == "setactive") {
//database functions
$sql = 'UPDATE clients SET active="1" WHERE id="'. $client .'"';
$result = query($sql);
confirmQuery($result);
//set message to user to display
$_SESSION['error'] = false;
$msgs[] = "Client was successfully restored!";
$_SESSION['userMsg'] = $msgs;
}
//display clients page with changes made
redirect('?page=clients');
}
function that displays the message.
//function that shows message and styles accordingly
function message($msgs) {
if(!empty($msgs)) {
foreach ($_SESSION['userMsg'] as $msg) {
if ($_SESSION['error'] == false) {
echo '<div class="noError">'.$msg.'</div>';
} else {
echo '<div class="error">'.$msg.'</div>';
}
}
}
unset($_SESSION['userMsg']);
unset($_SESSION['error']);
echo "hello";
}
here is where i call it in my page....
div class="pageContainer">
<?php
//display messages
message($_SESSION['userMsg']);
?>
</div>
Now when everything is run and i select to edit and client status (or add client, edit client as they all have the same message section, just with different message) i can see the message displayed on the screen. i have a javascript script that will hid the message after 5 seconds, but it doesn't hide. after viewing the page source i notice that the section of code is not visible. again, i can see it clearly see the message that is suppose to be there "Client was set to inactive and removed from the list!" (or whatever message is set to display) but it is NOT show in the view source html.
pic of page loaded with message
here is the view source.....
<div class="pageContainer">
<div>
** updated **
this small example script renders the html, but my above script doesn't...
function setError() {
$error[] = "Password field is to short";
$_SESSION['userMsg'] = $error;
//return $_SESSION['msg'];
}
function message($errors){
if(!empty($errors)) {
foreach ($_SESSION['userMsg'] as $error) {
echo $error.'<br>';
}
}
unset($_SESSION['userMsg']);
}
If I get you right, you want to display $msgs, so why you iterate $_SESSION['userMsg'] ?
Probably the $_SESSION['userMsg'] is null or empty. If you want to display the $msgs just iterate it. And later add to session if really needed. In you code, you always unset the $_SESSION['userMsg'] in the end of function, probably it is always null then. I didn't see the $_SESSION['userMsg'] needed here :
//function that shows message and styles accordingly
function message($msgs) {
if(!empty($msgs)) {
foreach ($msgs as $msg) {
if ($_SESSION['error'] == false) {
echo '<div class="noError">'.$msg.'</div>';
} else {
echo '<div class="error">'.$msg.'</div>';
}
}
}
unset($_SESSION['error']);
echo "hello";
}
UPDATED
After reading your comments, this is related to javascript problem. You should add the id in <div> in order for it's works.
if ($_SESSION['error'] == false) {
echo '<div id="noError" class="noError">'.$msg.'</div>';
} else {
echo '<div id="error" class="error">'.$msg.'</div>';
}
Had googled this but maybe I'm not a good googler. My ultimate goal is to have a view more button that will read the next 6 files from a directory but before I do that I need to figure out this problem:
at the top of my index.php i have this:
<?php
if(session_id() == '') {
session_start();
$_SESSION["count"] = 0;
}
?>
lower down I have this:
<?php
function album() {
$_SESSION["count"]=$_SESSION["count"]+10;
echo $_SESSION["count"];
}
?>
when the user clicks a button its supposed to print the session variable + 10 on each click.. so 10 20 30 40 ..ect. But it keeps printing 10, it's not updating.
Always start session at the top of the page like
<?php
session_start();
if(session_id() == '') {
$_SESSION["count"] = 0;
}
function album() {
$_SESSION["count"] += 10;
echo $_SESSION["count"];
}
?>
You need to first do session_start() and then ask for $_SESSION.
<?php
session_start();
if(empty($_SESSION['count'])){
$_SESSION["count"] = 0;
}
function album() {
$_SESSION["count"] = $_SESSION["count"] + 10;
echo 'Count: '.$_SESSION["count"];
}
album();
?>
This I just tested it and it works fine.
I am working on something that i want to use in a project.
i am to redirect the current page to itself using php and session. The idea is that:
if my `$_session['insert']= true; then refresh the page to itself;`
Here is the code:
sess_page1.php
session_start();
$_SESSION["insert"] = 1;
$_SESSION["test"] = "Page refreshed";
?>
Click here to go to the next page
sess_page2.php
<?php
session_start();
if (isset($_SESSION["insert"]) && ($_SESSION["insert"]==1)) {
header('location:'.$_SERVER['PHP_SELF']);
print $_SESSION["test"];
}
else
{
echo "Impossible to execute.";
exit();
}
?>
But the page does not redirect to itself...The whole idea is the page should refresh one time.
What i did wrong here? Can somebody help?
Try this:
sess_page1.php
session_start();
$_SESSION["stopper"] = 0;
$_SESSION["insert"] = 1;
$_SESSION["test"] = "Page refreshed";
?>
Click here to go to the next page
sess_page2.php
<?php
session_start();
if (isset($_SESSION["insert"]) && ($_SESSION["insert"]==1) && ($_SESSION["stopper"]==0)) {
$_SESSION['stopper']=1;
header('location:'.$_SERVER['PHP_SELF']);
print $_SESSION["test"];
}
else
{
echo "Impossible to execute.";
exit();
}
?>
I Initialized a $_SESSION variable = 1 and I want to increment/decrements its value when a link is click. And the links will reload the page then echo the value of the $_SESSION. I was given a hint redired using header() but I still can't figure it how.
<?php
if (isset($_SESSION['count'])) {
$count = $_SESSION['count'];
} else {
$count = '';
}
?>
Increment
Decrement
<?php if (isset($_SESSION['count'])): ?>
<?php echo $count ?>
<?php endif ?>
header("Location: index.php") is a php redirect. In this case if the code above is in the index.php file already, there's no need to redirect the user.
<?php
session_start();
if(!isset($_SESSION['count']) { // first time opening the page
$_SESSION['count'] = 0; // initializing the counter
} else { // counter already have a value
if(isset($_GET['inc'])) { // increasing
echo ++$_SESSION['count']; // no need for extra variable (preincrement to echo immediately)
}
if(isset($_GET['dec'])) { // decreasing
echo --$_SESSION['count']; // no need for extra variable (predecrement to echo immediately)
}
}
?>
Increment
Decrement
<?php
session_start();
if(!isset($_SESSION['count'])
$_SESSION['count'] = 0;
$counter = $_SESSION['count'];
$counter = (int)$counter;
if (isset($_GET['inc'])==TRUE) {
$counter++;
$_SESSION['count'] = $counter;
header("Location: index.php");
}
if (isset($_GET['dec'])==TRUE) {
$counter--;
$_SESSION['count'] = $counter;
header("Location: index.php");
}
?>
Also You may need to cast $counter to int..
i have a for loop that will loop through a set of page in php.
for ($count=0;$count<=$curr;$count=$count + 10)
{
if ($find==1) {
$result = "file.php?count=$count";
}else {
$result = "file2.php?count=$count";
}
$match = file_get_contents($result);
$nc=$count+10;
if (preg_match("/\b$file\b/i", $match)) {
print "found in $count";
} else {
print "not found in $count";
}
}
the problem is that the result is displayed after the last page is executed, since it will loop through 500 pages which takes more time. so how can i make this code display the print result as it is executed each cycle,
while(something)
{
// do something
echo "Hi";
flush();
}
Using the flush() function will output everything to the browser that has been sent so far (sent as in echo, print or other similar functions.).