Jump to content

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

 

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.