-2

Hey guys am new to php actually..I have an input form and when the user types something in that input form it displays.what i want to do is, after a minute i want the session to be expired and redirect to another website..I know this can be done easily with javascript..But i would like to do it with php sessions..So the code i have written is

<?php
if ( isset($_POST['firstname'])) {
  echo $_POST['firstname'];
  session_start();
  $_SESSION['logintime'] = time();
  if(time() - $_SESSION['logintime'] > 60) {
     header('Location: http://www.google.com/');
  } else {
    echo 'blah';
  }
}
?>

When i run the code i gets the inputname and blah(which means the second if statement not working).

Hope you guys could help me out..Any help would be appreciated ..Thanks

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
phplover
  • 1
  • 2
  • 4
    `session_start()` must come before any output. – Jay Blanchard Feb 06 '15 at 14:55
  • 2
    possible duplicate of [How do I expire a PHP session after 30 minutes?](http://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes) – runDOSrun Feb 06 '15 at 14:55
  • *echo $_POST['firstname']; session_start();* i think you read the manual and know, that **no output** has take place before session_start() – donald123 Feb 06 '15 at 14:55
  • @JayBlanchard i didnt get what you mean ..do i want to add session_start() at the very top of the code ?? – phplover Feb 06 '15 at 14:58
  • 1
    Yes @phplover. Past that point, as others have mentioned, you need something other than PHP to do the timing because PHP is server-side and once your page has rendered it has no awareness of what is going on on the server. – Jay Blanchard Feb 06 '15 at 15:00
  • @JayBlanchard so for this action to take place do i need to use js too ??. – phplover Feb 06 '15 at 15:02
  • Yes, you will need to use JS for this and you'll have to send some information to PHP, via AJAX perhaps, to destroy the session with `unset()`. – Jay Blanchard Feb 06 '15 at 15:16
  • @JayBlanchard thanks for the info but i have something to ask you ..then what does `if(time() - $_SESSION['logintime'] > 60) { some code }` reprsents ..it checks if a session is expired ..right ??..so do i want to add those js script inside the if statement.?? – phplover Feb 06 '15 at 15:30
  • It doesn't check if the session has expired, it just checks the original time against the current time. You can get the original session time as a variable to use in your JavaScript. – Jay Blanchard Feb 06 '15 at 15:32
  • @JayBlanchard could you post an idea or a sample code as an answer it would really help me .. – phplover Feb 06 '15 at 15:34

2 Answers2

0

"after a minute" .. this will not work. PHP is server side, so you need to interact with the server (like with a page load) before PHP can control anything. The only way to do what you're describing is with Javascript.

jdu
  • 581
  • 2
  • 3
0

Here is some pseudo-code, i.e. not tested at all, to demonstrate:

PHP

<?php
session_start();
$_SESSION['logintime'] = time(); // when the login occurs
?>

JavaScript

<script>
// using jQuery
$(function(){
    var loginTime = <?php echo $_SESSION['logintime`]; ?>; // places PHP variable into JS variable
    var testSession = setInterval(function(){
        var currentTime = Date.now() * 1000; // convert to UNIX time for easy math
        if((currentTime - loginTime) > 60) {
            // ajax to request session unset
            $.post('clear_session.php', function() {
                console.log('session destroy request');
            });
            window.location.href = 'wherever.html'; // redirect
        } 
    }, 1000); //test each second
});
</script>

clear_session.php

<?php
    session_start();
    // belt and suspenders
    session_unset();
    unset($_SESSION);
    session_destroy();
?>
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
  • i didnt get what you mean by those `belt and suspenders` ..and why do you made a seperate file for this ??.i think one file is enough.. – phplover Feb 06 '15 at 17:13
  • "Belt and suspenders" means I am doing everything I can to make sure the session is destroyed. The separate file is for maintainability. – Jay Blanchard Feb 06 '15 at 17:39
  • this code isnt working though ??..you have used `if((currentTime - loginTime) > 60)` which is same as ` if(time() - $_SESSION['logintime'] > 60) {` ?? – phplover Feb 06 '15 at 17:48
  • Like I said, it is pseudo-code. What does do you mean by "isn't working"? Are there errors being reported? Have you included jQuery in your project? – Jay Blanchard Feb 06 '15 at 17:49
  • when i tried adding the js code inside the php file like echo 'the js code' it throws error – phplover Feb 06 '15 at 17:50
  • That is out of the scope of what we're doing here. You shouldn't be echoing JavaScript code. You will need to have another question for that, but check for duplicates first as there are many "echo javascript with PHP" questions already out there. – Jay Blanchard Feb 06 '15 at 17:52
  • could you please edit your answer into an appropriate form ..i removed the echo bu its still showing error ..it would be really thankful if you edit the answer you have provided – phplover Feb 06 '15 at 18:00
  • What do you mean by "appropriate form"? Since I had nothing to work with an you asked for a sample it is appropriate, isn't it? ¯\\_(ツ)_/¯ – Jay Blanchard Feb 06 '15 at 18:06
  • am confused to add the js with that php file..could you help me in that by editing ur answer – phplover Feb 06 '15 at 18:07
  • My answer has you creating a PHP page that has JS in it. Are you wanting me to show the whole HTML markup? At this point we have moved way beyond the scope of your original question. – Jay Blanchard Feb 06 '15 at 18:10
  • i have fixed some errors and the code is working ..but it isnt redirecting after a minute – phplover Feb 07 '15 at 06:28
  • Check the values for `loginTime` and `currentTime` to make sure the subtraction you're expecting occurs. – Jay Blanchard Feb 09 '15 at 12:50
  • yes i have taken the same value you have given me in this answer ? – phplover Feb 11 '15 at 02:12
  • You either need to post a new question with the new code that you're using or edit your original post to add the updated code so we can see where the issue is. – Jay Blanchard Feb 11 '15 at 12:50