Help with issue.

INeedAGig

New Member
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. I'm sure this is a simple cookie issue, but I'm not quite sure how to do it.
 

smoovo

New Member
First WELCOME :).

Now, you are definitely right about this, you need to use cookies, because there is no way to find out for sure if this user have already fill this form in the past. The cookie will save his details and time, and next time he will log from that machine, the page will look for the cookie, if it's there it will get all of his information from the saved variables.

If you don't know how to, just use this tutorial.


- Goog Luck. ;)
 

INeedAGig

New Member
Thanks for the reply and the link. I still have a couple questions though.

How would I go about getting this to work? Here is my setup so you have a more clear idea:

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.

Thanks!
 

smoovo

New Member
If i got it right, you have a statement that checks if the user already filled the form, if yes its re-direct the user to the main page.

So, the first step is to change the statement to check if there is a cookie (you will name it whatever you like) exist on the user's machine. If yes it will re-direct to the main page, if not it will re-direct to the form page. This statement should be placed in your PHP check script.

Second step is to store the cookie on the user's machine if it's not exist. This will be fired right after the form is been filled for the first time.

The cookie can be named by any name, and can contain any variable name you like. But, use the same names to store and to check. For example, you can call it "$_COOKIE['form']" and it will contain variable called "filled".

To store it...
PHP:
setcookie("form", "filled");

To check it...
PHP:
if(isset($_COOKIE["form"])) {...}

I hope it will help... :)
 

INeedAGig

New Member
Well, the redirect occurs after the user physically hits the "Submit" button. I want to be able to have an automatic re-direct to the main page occur upon future visits to the page without the user having to physically hit the "Submit" button, or even really see the page for that matter. Again, this would only happen after the page knows the user already filled out the form before (cookie)


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.
 

smoovo

New Member
So you should add the PHP statement that i have showed you for the main page. It will check the cookie before the page is loaded, and if it has the cookie on his machine it will re-direct the user to the second page without showing the form.

PHP:
if(!isset($_COOKIE["form"])) {

    // Here comes your whole form page code.
    // Inside the form page you should add cookie store.
    // If submitted setcookie("form", "yes");

} else {

    header("location: /page2.html");

}

This is it. ;)
 
Last edited:

INeedAGig

New Member
What is the proper location for the isset code placement? inside or outside of the <html> tags on the form page?
 

smoovo

New Member
In this case outside, i have posted you the exact code with comment where you should place all the page.
 

INeedAGig

New Member
I have this before any other coding on the index.php page (the page that needs to redirect if they have the cookie)

PHP:
<?php 
if(isset($_COOKIE['user'])) { 
header ('Location: main.php');
}
?>

I have verified that the cookie is set, but it doesn't want to redirect.
 

smoovo

New Member
If you have filled the form, you should check if the "setcookie" worked. Instead of the script line
PHP:
header("Location: main.php");
put
PHP:
echo $_COOKIE['user'];
This will do the check. If there is a cookie on your machine you will get the variable value (in my example it was 'yes'). If it is set you should look for a problem with the header, maybe misspelling or no such a path. If not, your problem is with the "setcookie" function.
 
Top