ROCKINDANO Posted September 10, 2011 Share Posted September 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/246860-need-help-with-sessions-for-first-time-visiters/ Share on other sites More sharing options...
mikesta707 Posted September 10, 2011 Share Posted September 10, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/246860-need-help-with-sessions-for-first-time-visiters/#findComment-1267818 Share on other sites More sharing options...
ROCKINDANO Posted September 10, 2011 Author Share Posted September 10, 2011 can someone point me to a good example of what i am trying to do, but using cookies? Quote Link to comment https://forums.phpfreaks.com/topic/246860-need-help-with-sessions-for-first-time-visiters/#findComment-1267861 Share on other sites More sharing options...
gizmola Posted September 11, 2011 Share Posted September 11, 2011 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(); } Quote Link to comment https://forums.phpfreaks.com/topic/246860-need-help-with-sessions-for-first-time-visiters/#findComment-1267867 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.