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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.