Jump to content

Help with cookie redirect on PHP form page.


INeedAGig

Recommended Posts

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! :)

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.