Jump to content

need help with sessions for first time visiters


ROCKINDANO

Recommended Posts

I need some assistance with sessions

 

i am using sessions for first time visitors. first time visitors that visit my website will be see a welcome page for them to sign up for emails and once they return to my site they go straight to my home page.

 

this is what i have as for a session coding:

<?php
session_start();



$visits = $_SESSION['visits']++;
if ($visits == 0) {
       header ('location: welcome.php'); 
   
}
else {
       header ('location: index.php'); 
}
?>

 

i have this in my index.php file.

 

any advice on what i am doing wrong?

Everytime the web browser is closed or the connection to the web server is in any way ended, the session data is disgarded. You probably want to use a cookie rather than a session.

 

Also,

$_SESSION['visits']++;

 

will fail unless that value already exists. for first time users it won't exist, and will generate an error. You need some sort of if statement to detect if they are first time users (probably using isset() with $_SESSION['visits'] as the parameter) and if its their first time then set visits to 1 (or 0, whichever value would make more sense to you)

 

Simply read about $_COOKIE and setcookie

 

Something like this:

 


if (isset($_COOKIE['visited']) {
  header ('location: index.php');
  exit();
} else {
  setcookie('visited', 1, time() + 31536000);
  header ('location: welcome.php');
  exit();
}

 

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.