Cathryn Posted June 22, 2011 Share Posted June 22, 2011 Hello, i am trying to write a program using sessions that states if its the user's 1st visit. If its not it should write the date and time of the last visit. This is the code i have but it is not working past the 1st visit. Any help would be much appreciated. <?php session_start(); if(($_SESSION['views'])== 1){ echo "This is your first visit."; ?> <?php } ($_SESSION['views']+ 1) else(($_SESSION['views'])< 1){ echo "This is your visit"<br>; echo "You previously visited this page on "; echo date("d F Y"); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240164-php-sessions/ Share on other sites More sharing options...
requinix Posted June 23, 2011 Share Posted June 23, 2011 That's not valid PHP code... What, exactly, do you have? Quote Link to comment https://forums.phpfreaks.com/topic/240164-php-sessions/#findComment-1233609 Share on other sites More sharing options...
Cathryn Posted June 23, 2011 Author Share Posted June 23, 2011 This is what i am trying to get. For the 1st session: This is your first session. And for the others after that: Your previous session was on: 21 Jun 2011 11:52:05 21 Jun 2011 12:50:21 21 Jun 2011 12:32:45 I understand i need a for loop to get the times to record but the code i have is ending in the first session. <?php session_start(); $_SESSION['views']=1; if(($_SESSION['views'])==1){ echo "This is your first visit"; } elseif(($_SESSION['views'])> 1){ echo "You previously visited this page on: "; for($i=1;$i > 1; $i++){ echo date("D, d F Y - G:i:s"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/240164-php-sessions/#findComment-1233672 Share on other sites More sharing options...
Alex Posted June 23, 2011 Share Posted June 23, 2011 When posting code please use [[/tt]php] or [[tt]code] tags. Here's an example, along with an explanation, of how to do what it seems like you're after. <?php // start the session session_start(); // check to see if the session variable, $_SESSION['visits'], exists. // $_SESSION['visits'] will be an array containing timestamps of the visits in this session if(!isset($_SESSION['visits'])) { // if this variable isn't set yet.. then we want to set it, and we'll store the current timestamp as the first element $_SESSION['visits'] = array(time()); echo "This is your first visit!"; } else { // $_SESSION['visits'] already exists, so they've visited in this session before // first, we'll print out all the previous visits foreach($_SESSION['visits'] as $visit) { echo date("D, d F Y - G:i:s", $visit); } // now, we'll add this visit's time stamp to the array $_SESSION['visits'][] = time(); } Quote Link to comment https://forums.phpfreaks.com/topic/240164-php-sessions/#findComment-1233686 Share on other sites More sharing options...
Cathryn Posted June 23, 2011 Author Share Posted June 23, 2011 Thank you so much, the code you sent helped with what i needed. Quote Link to comment https://forums.phpfreaks.com/topic/240164-php-sessions/#findComment-1233712 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.