INeedAGig Posted December 8, 2010 Share Posted December 8, 2010 First, I would like to say hi to everyone, I just registered, so I am new to the forums! But at any rate, I have a question. I have a form that I have programmed in PHP and upon completion of the form, sends the inputed information to my clients e-mail (acts as a lead) and redirects the user to the main part of the website, where you can access all of the content. Now, I need a little assistance on how to make it so if the same user comes back to the site again, they won't always have to fill out the form and it will just forward them to the main page. Clarification: This is what I'm trying to figure out When you go to the url for the first time, the index page has the form on it that must be filled out before you can access the rest of the pages on the website (I have this set as a redirect in the php processing after you submit the form). What coding would I have to add to the html on the form page (index) and/or my php processor and/or the html on the page you are redirected to? My overall goal is for the user to only see the form page once, after they have submitted it the first time, the next time they visit the url, I want them automatically re-directed to the main part of the site since they already completed the form. For Example: (I have this part programmed and working already) The user goes to http://www.example.com for the first time and they see the page with the form on it. They fill it out and click "Submit" and are re-directed to http://www.example.com/page2.html, this redirect is handled with a header "location" command. (This is the part I need help with) At a future time the same user returns to http://www.example.com and since they have already filled out the form they are automatically re-directed to http://www.example.com/page2.html without having to hit the "Submit" button or really even see the form page. Thank you very much in advance, I appreciate your help! Quote Link to comment Share on other sites More sharing options...
wigpip Posted December 8, 2010 Share Posted December 8, 2010 assuming you have set a cookie based on something they entered in the submitted form then check if cookie set and redirect if(isset($_COOKIE['mywebsite_your_name']) { header ("Location: http://" . $_SERVER['HTTP_HOST'] . "/pagename.php"); exit(); } hope it helps www.wigpip.com.au Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 8, 2010 Author Share Posted December 8, 2010 Thanks for the reply. I need help getting the cookie set as well and the proper place to put the code for it. I haven't really messed with cookies before so it is new to me. Thanks! Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 I would suggest then you start at the manual and/or the php tutorial on http://w3schools.com. Specifically, once a user fills in the form prior to them being redirected you set a cookie (see the manual). The cookie should have a single variable return with a value of 1. Then when you check that variable exists, and redirect to the main page or the form if it doesn't. This is very similar to the code already posted: if(isset($_COOKIE['return']) { header ("Location: http://" . $_SERVER['HTTP_HOST'] . "/page2.html"); exit(); }else{ //redirect to the form } Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 8, 2010 Author Share Posted December 8, 2010 Thank you for the replies. I am having a bit of trouble with the retrieval of the cookie. When I place the isset code on the form page, it is just returning a blank white page when I go to it in my browser, any tips? Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 8, 2010 Author Share Posted December 8, 2010 bump Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 We need to see the exact code you are using for the form page which is checking the cookie exists. Should then be able to see why you are seeing a blank page. In my example, if you haven't included anything where my comment is, and a cookie doesn't exist, you will see a blank page also. Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 8, 2010 Author Share Posted December 8, 2010 Okay, now it is just loading the index.php page (the form page) no matter what, I did check and the cookie is there. Here is the code I am using on the index page with the form to make sure the cookie is there. This is in between the <html> and <head> tags on the page. <?php if(isset($_COOKIE['user'])) { header ("Location: http://" . $_SERVER['HTTP_HOST'] . "/main.php"); exit(); }else{ header ("Location: http://" . $_SERVER['HTTP_HOST'] . "/index.php"); } ?> Here is the code to set the cookie at the very top of my form processor (this is working, with the exception that it expires when the browser is closed for some reason) <?php $expire=time()+60*60*24*120; setcookie("user", $expire); Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 From the php manual regarding setcookie(): setcookie("user", "1", time()+3600, "/"); Your cookie params are wrong. Try the above and see if it works. If a cookie is detected you need to echo the cookie. In fact, it is good practice in these situations to echo most steps to ensure you know exactly what is going on. After a while you develop a sense of what is wrong before you even test the code. However, in the beginning it can be very beneficial. Do an echo of each variable or the variable you are testing. Echo the cookie(s), echo 'after if' after an if statement. Just try to discover what is wrong by seeing what you are putting in vs what is being returned. Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 8, 2010 Author Share Posted December 8, 2010 I changed the setcookie to what you suggested and now when you go to the form page it is returning blank. Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted December 8, 2010 Share Posted December 8, 2010 You need to try to make it 'not blank' by echoing something. You need to know exactly where you code is getting. Is the if statement true? if a cookie is found, print the cookie to screen. if it isn't echo something like 'no cookie found'. You need to know exactly what is validating and what is not. Saying the page is blank is the hardest possible thing to resolve. it could be code, could be lack of code, could be because no error reporting is on, could be browser problem, could be a lot of things. Please, paste your code you are using. The exact code so we can better help you. Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 8, 2010 Author Share Posted December 8, 2010 Which code would you like to see? Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 8, 2010 Author Share Posted December 8, 2010 I'll try to clarify one more time exactly what I am trying to do. I appreciate all of your help so far! I'm just a little confused with the cookie portion of it. The user goes to www.example.com in their browser, and they see the homepage (index.php) just like they would any website and since it is the first time they are visiting the website, they do not have access to any other part of the website until they complete the form. Upon completion of the form they are re-directed (via the form processor) to the page where they can now access all of the content on the website, keep in mind, this is not a user system where they will need to log in and out, just a way of getting the user to fill out the form so they can access content. Now, this is where I need the help, the cookie. For example, the same user returns to the site via www.example.com a couple days later and since they already filled out the form at a prior time they are automatically redirected to the content page, which essentially is now the homepage for them. Keep in mind, the form is on the homepage (index.php), if its a first time visitor I want index.php to load just like it always would, if it's a return visitor then I want content.php to load, which is essentially the return visitors homepage. Here is some of the code I have, so you have a reference. This cookie retrieval string is on the index.php page right after the <body> tag <?php if(isset($_COOKIE['user'])) { header ("Location: content.php"); exit(); }else{ header ("Location: index.php"); } ?> This is the first string of code in the form processor to set the cookie (this is working) <?php setcookie("user", "1", time()+3600*30000, "/"); I have been testing and testing, clearing the cookie everytime, moving the spots where I am putting the cookie retrieval code in the index.php, etc. It works to set the cookie the first time, but every visit after that is either a blank page or always loading index.php Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 9, 2010 Author Share Posted December 9, 2010 bump Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 9, 2010 Author Share Posted December 9, 2010 Any ideas? Thanks! Quote Link to comment Share on other sites More sharing options...
INeedAGig Posted December 9, 2010 Author Share Posted December 9, 2010 SOLVED. It was simple actually, didn't realize I had the PHP file encoded in UTF-8 instead of ANSI, blah! 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.