Jump to content

php sessions


Cathryn

Recommended Posts

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");
	}
?>

Link to comment
https://forums.phpfreaks.com/topic/240164-php-sessions/
Share on other sites

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");
}
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/240164-php-sessions/#findComment-1233672
Share on other sites

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();
}

Link to comment
https://forums.phpfreaks.com/topic/240164-php-sessions/#findComment-1233686
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.