zlatangu Posted August 17, 2014 Share Posted August 17, 2014 Hi, I want to show one image for the new visitors of my website and another for the returning visitors. What I want to do is that after the visitor reload the page he will see another image instead of the first one. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 17, 2014 Share Posted August 17, 2014 How are you tracking visitors? With a login process perhaps? You have to know some way of who's who, unless you simply want to track the ip address, which won't be at all accurate. So - you log someone in. If they already existed, you change the background on the next page load. Simple? Quote Link to comment Share on other sites More sharing options...
zlatangu Posted August 17, 2014 Author Share Posted August 17, 2014 How are you tracking visitors? With a login process perhaps? You have to know some way of who's who, unless you simply want to track the ip address, which won't be at all accurate. So - you log someone in. If they already existed, you change the background on the next page load. Simple? No you are not understanding me. I can use cookie for letting the script know that the visitor have been on that page before and than load the second image instead of the first one. Just that I don't know to code in php, can you help me please. Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted August 17, 2014 Solution Share Posted August 17, 2014 You'd use setcookie to set the cookie Then using isset to check if the cookie exists in the $_COOKIE superglobal. If the cookie exist you'd show the second image otherwise show the first image Read the documentation to the linked pages above, study the code examples to see how they are used. From the pseudo code below you show be able to implement what you are trying to do in PHP /*before any output*/ set the 'visited' cookie /* in your HTML page you'd have this if statement*/ if(isset cookie 'visited') { /*output second image*/ } else { /*output first image*/ } Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 17, 2014 Share Posted August 17, 2014 Your first post did not mention that you did not know how to program. Try to be a little more informative in your next post - of which I'm sure there will be many as you embark on learning how to write PHP scripts. Quote Link to comment Share on other sites More sharing options...
zlatangu Posted August 17, 2014 Author Share Posted August 17, 2014 You'd use setcookie to set the cookie Then using isset to check if the cookie exists in the $_COOKIE superglobal. If the cookie exist you'd show the second image otherwise show the first image Read the documentation to the linked pages above, study the code examples to see how they are used. From the pseudo code below you show be able to implement what you are trying to do in PHP /*before any output*/ set the 'visited' cookie /* in your HTML page you'd have this if statement*/ if(isset cookie 'visited') { /*output second image*/ } else { /*output first image*/ } OK from the following code i got just a blank page <?php /*before any output*/ set the 'visited' cookie /* in your HTML page you'd have this if statement*/ if(isset cookie 'visited') { print ("second") /*output second image*/ } else { print ("first") /*output first image*/ } ?> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 17, 2014 Share Posted August 17, 2014 (edited) As Chocu3r said "From the pseudo code below...". What you wrote is NOT code. You simply echoed the 'pseudo code' he wrote. Do you not know the meaning of 'pseudo'? You really need to learn how to write php. That means reading. And reading some more. Did I mention learning? Edited August 17, 2014 by ginerjm 1 Quote Link to comment Share on other sites More sharing options...
zlatangu Posted August 17, 2014 Author Share Posted August 17, 2014 Thanks for help i finally learned how to do that. <?php setcookie("user", time()+3600); ?> <html> <body> <?php if (isset($_COOKIE["user"])) print ("<IMG SRC =image1.PNG>"); else print ("<IMG SRC =image2.png>"); ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 17, 2014 Share Posted August 17, 2014 actually you haven't learned. Yes you wrote some rudimentary php code but it is flawed. Besides the intermingling of html and php code needlessly, you did not read the entire manual page covering setcookie. If you had you would realize that you cannot set a cookie and 5 lines later check for its existence. Cookies are not 'real' until the page has refreshed. And I'm pretty sure you didn't read all the documentation since your cookie is malformed to begin with. And to top it all off the html you are outputting for your image display is also bad. Quote Link to comment Share on other sites More sharing options...
zlatangu Posted August 17, 2014 Author Share Posted August 17, 2014 actually you haven't learned. Yes you wrote some rudimentary php code but it is flawed. Besides the intermingling of html and php code needlessly, you did not read the entire manual page covering setcookie. If you had you would realize that you cannot set a cookie and 5 lines later check for its existence. Cookies are not 'real' until the page has refreshed. And I'm pretty sure you didn't read all the documentation since your cookie is malformed to begin with. And to top it all off the html you are outputting for your image display is also bad. Yes what you are saying is true. But for now this was what I needed for my website. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted August 18, 2014 Share Posted August 18, 2014 Good luck. You came for help. I don't feel I've helped you and I presume you have left the building. I feel sad. Again - good luck. Quote Link to comment 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.