turk Posted July 15, 2011 Share Posted July 15, 2011 hi fellas. my first ever question on phpfreaks. when visitors type www.mydomain.com, session takes them to www.mydomain.com/landingpage.php when they click "go to the site" on www.mydomain.com/landingpage.php, it takes them to the index page www.mydomain.com Unless they clean their cooking, session won't forward take visitors to the landingpage ever again. but is there anyway to show the landing page to visitors, lets say the first 5 times when they type www.mydomain.com? here is what i exactly use for it. <?php session_start(); if(!isset($_SESSION['views']) || !$_SESSION['views']) { $_SESSION['views'] = 1; header("location: /landingpage.php"); exit; } ?> Much Thanks in advance. Turk. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/ Share on other sites More sharing options...
premiso Posted July 15, 2011 Share Posted July 15, 2011 <?php session_start(); // default the variable to avoid a notice error: $_SESSION['views'] = !empty($_SESSION['views'])?$_SESSION['views']:0; if ($_SESSION['views'] < 5) { $_SESSION['views'] += 1; header("location: /landingpage.php"); exit; } ?> Should do it. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243146 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 ..nice. thanks alot premiso. it worked. but it kinda makes loop :-\ it brings the .com/landingpage.php without showing /index.php 5 times. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243151 Share on other sites More sharing options...
djlee Posted July 15, 2011 Share Posted July 15, 2011 This would be more suitable, so you dont badger the visitor too much in quick concession. <?php session_start(); //-- times before landing page is disabled $lpcount = 5; //-- time between landing page shows $halt_time = 60 * 10; //10 minutes // default the variable to avoid a notice error: $_SESSION['views'] = !empty($_SESSION['views'])?$_SESSION['views']:0; $_SESSION['last_land'] = (int)$_SESSION['last_land'] > 0 ? $_SESSION['last_land'] : 0; if ($_SESSION['views'] < $lpcount && $_SESSION['last_land'] < (time() - $halt_time)) { $_SESSION['views'] += 1; $_SESSION['last_land'] = time(); header("location: /landingpage.php"); exit; } ?> However if you just want to stop the loop then add a check in the IF() that says "if http_refferer is my own site, then do not redirect to landing page" Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243159 Share on other sites More sharing options...
premiso Posted July 15, 2011 Share Posted July 15, 2011 it brings the .com/landingpage.php without showing /index.php 5 times. I am having a real problem with reading shit today: <?php session_start(); // default the variable to avoid a notice error: $_SESSION['views'] = !empty($_SESSION['views'])?$_SESSION['views']++:0; if ($_SESSION['views'] > 4) { header("location: /landingpage.php"); exit; } ?> That should do what you want...hopefully. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243162 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 are you saying that you want the first 5 views of a user to be directed to your index, then the rest to be redirected, or something else..? Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243164 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 WOW. first of all very impressed by all these help. u guys are fucking great. I love the freaks already. @djless clever. i really like the clock. i dont know why but it works as my session code. it wont do the count. :/ @premiso this session did not redirect me to landingpage.php for some reason are you saying that you want the first 5 views of a user to be directed to your index, then the rest to be redirected, or something else..? yes. when users type the root domain, instead of index.php, i want visitors to be directed /landingpage.php at least 5 times. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243171 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 premiso's code should do what you need it to do, with a slight adjustment. <?php session_start(); // default the variable to avoid a notice error: $_SESSION['views'] = (!empty($_SESSION['views'])) ? $_SESSION['views']++ : 0; if ($_SESSION['views'] > 4) { header("Location: landingpage.php"); exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243177 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 i see the change. tried few other things but it still hits index.php without redirecting to landingpage.php Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243179 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 try printing the session value to see what it's actually doing Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243182 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 very tough. it is printing only Array ( [views] => 0 ) i might be wrong but it seemed to me like, it gets directed to landingpage.php but before even load it get redirected to back to index.php Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243186 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 if prints out a value of 0 each time you revisit the page? Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243189 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 it sure does. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243205 Share on other sites More sharing options...
AyKay47 Posted July 15, 2011 Share Posted July 15, 2011 okay we are going to do it the if else way <?php session_start(); // default the variable to avoid a notice error: if(empty($_SESSION['views'])){ $_SESSION['views'] = 0; }else{ $_SESSION['views'] = $_SESSION['views'] + 1; } if ($_SESSION['views'] > 4) { header("Location: landingpage.php"); exit; } ?> pretty much the same thing here, not sure why the ternary would't work so i want to make sure something funky isn't happening Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243208 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 sorry for the headache im causing AyKay47. why simple looking things always has to cause the major headache. no luck with the if statement either chief. getting the same print "Array ( [views] => 0" Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243214 Share on other sites More sharing options...
TeNDoLLA Posted July 15, 2011 Share Posted July 15, 2011 Should not this anyway be if ($_SESSION['views'] > 4) instead like this if you want to show landing page five times but not more if ($_SESSION['views'] <= 4) Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243216 Share on other sites More sharing options...
DavidAM Posted July 15, 2011 Share Posted July 15, 2011 The empty() function will return true when the value is zero. So you need to use an isset() call there: $_SESSION['views'] = (isset($_SESSION['views'])) ? $_SESSION['views']++ : 0; Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243225 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 thank you david and tendola. updated the session as follows, and now it only stays on the /landingpage.php <?php session_start(); $_SESSION['views'] = (isset($_SESSION['views'])) ? $_SESSION['views']++ : 0; if ($_SESSION['views'] <= 4) { header("Location: landingpage.php"); exit; } ?> P.S. glad asked you guys, this much testing would have taken me over a week. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243260 Share on other sites More sharing options...
xyph Posted July 15, 2011 Share Posted July 15, 2011 Just to make sure you know - unless you have explicitly told PHP to keep sessions going beyond a browser close or a short period of time - this won't keep the value after the use closes their browser. The next time they visit your site they'll be taken to landingpage.php another 5 times automatically. A cookie would be better if you wanted to hold that count beyond a single browser session. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243291 Share on other sites More sharing options...
turk Posted July 15, 2011 Author Share Posted July 15, 2011 i want users to be taken to the landingpage.php each time when they visit the site for the next 5 times they visit the site. the existing code i previously had (which is down below) works like a charm but brings the landingpage.php only once. <?php session_start(); if(!isset($_SESSION['views']) || !$_SESSION['views']) { $_SESSION['views'] = 1; header("location: /landingpage.php"); exit; } ?> here it goes in this code. user types www.mydomain.com and get redirected to www.mydomain.com/landingpage.php where clicks on HOME (<a href="www.mydomain.com">) button and goes to www.mydomain.com closes to browser, and next day user types www.mydomain.com and gets directed to www.mydomain.com what i need is that users redirected to the /landingpage.php at least the first 5 times they visit the site. P.S. i hope, i made more sense this time. this could be very strategic and useful to capture user emails, advertise promotional, recent news, info video, etc. user gets fully familiar with the site. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243302 Share on other sites More sharing options...
AyKay47 Posted July 16, 2011 Share Posted July 16, 2011 xyph brings up a good point which I have overlooked. Lets say that you got the code to work with sessions, an the user visits your site 6 times without closing the browser and the 6th time they are taken to the homepage or wherever you are directing them. When the user closes the browser and reopens, the session information will be erased, thus the session value will be reset to 0, starting your count all over again. I doubt that this is your desired intentions, perhaps you should use cookies for this instead and overwrite the cookie value each time a user visits the page... Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243500 Share on other sites More sharing options...
djlee Posted July 16, 2011 Share Posted July 16, 2011 the best option would be to use the http referer. that way they dont get annoyed browsing your site but if they type it in the url then they get redirected. Put this in a core file thats included everytime and no matter what url they type in they will be redirected to the landing page by default. Put it only on the index page and it will only redirect to landing page if index is visted from somewhere other than your own site. Which ever one is more in line with your site i guess, ive used both ways in the past. Quote Link to comment https://forums.phpfreaks.com/topic/242070-can-visits-multiplied-in-php-session/#findComment-1243520 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.