Hi guys,

Now I'm a bit of a newbie and no-one has answered my topic in the newbie area so I'm turning to you guys for help.

I would like to create a script that redirects the visitor to different pages based on the value of a stored cookie. If no cookie is found, the available items are displayed for the user to pick from, which sets a cookie for their next visit.

The reason for this is that I am doing a locale based website with town areas, and I wish for the user to be able to select which town they are in and it be set so that next time they skip the selection and get to the page for their locale.

Could anyone do me the biggest favour and provide me with sample code? I hate pushing but I need this soon, I have only managed to create a simple feedback form, that took me a week and some help from here so please explain the stuff your doing if you are going to help me.

Cheers guys I hope someone helps this time!!

    Do your form, and when you go into the "results" page from your form, set your cookies in the header area.. look at:
    http://www.php.net/manual/en/function.setcookie.php for the syntax for it, has a few good little bits on there too. you have to remember where you set them if you leave the string path empty though, and very easy to call the info, just like $anything if that is the cookie name....

      I don't get it, please clarify with some example code....aggghhh lol

      Cheers

        OK - try this one out....

        <.. snipped code.. >
        <form name=form1 method=post action=results.php>
        <input type=text name=area>
        <input type=submit name=submit value=go>
        </form>
        
        --__--__--__--__--__
        RESULTS.PHP
        --__--__--__--__--__
        <?php
        SetCookie("area",$area); //sets cookie with area put in the first form but it will delete when the browser is closed as there is no expiry time.
        //the rest of your code lies here
        ?>
        

        To use the value in the cookie - just echo $area; and it will show up...

          How do I get it to check for that cookie and redirect accordingly though next time they visit?

          Cheers

            You need to set an expiry time, as

            setcookie ("TestCookie", $value,time()+3600);  // expire in 1 hour
            

            Note the use of time()+3600, count in seconds to work it out...

            and using

            if (!isset($TestCookie)) {
            print("Cookie not set");
            exit;
            }
            print("Cookie of $TestCookie has been set");
            exit;
            ?>
            

            Something like that anyway...

              Ok, lemme see if I got this straight, would the below work? Bare with me and correct me as required....

              
              if (!isset($TestCookie)) {
              
              // Form code here...
              
              }
              
              else if ($TestCookie == true) {
              
              // header code here passing on to redir.php (redirect page)
              
              }
              
              

              Then on the redir.php....

              
              header("Location: http://$TestCookie.example.com/"); // because I'm using sub-domains...
              exit;
              
              

              Am I right or am I wrong? If you can't use variables in the header, I could always echo a meta refresh one with it in cudn't I?

              Please let me know asap!!

              Cheers for your help thus far !!

                Never mind I got it working myself, here is how I have done it for anyone else that wants this kind of thing.

                In the file index.php, I have written;

                
                <?php
                
                if (!isset($redirect)) {
                
                echo ("
                <form method='post' action='setcookie.php' name='barns_select'> 
                <input type='hidden' name='barns'><input type='Submit' name='redir' value='barnsley'>
                </form>
                
                <form method='post' action='setcookie.php' name='shef_select'>
                <input type='hidden' name='shef'><input type='Submit' name='redir' value='sheffield'>
                </form>
                
                ");
                }
                
                else if ($redirect == true) {
                
                header("Location: redir.php");
                
                }
                ?>
                
                

                In setcookie.php, I have written;

                
                <?php
                
                setcookie ("redirect", $redir,time()+3600);  // expire in 1 hour
                header("Location: redir.php"); 
                
                ?>
                
                

                And finally, in redir.php, I have written:

                
                <?php
                
                echo ("
                
                <html> 
                <head> 
                <meta http-equiv='refresh' content='4;url=http://www.$redirect.co.uk/'> 
                <title>Redirect</title> 
                </head> 
                
                <body> 
                Redirecting.....hold on to your seats !!
                </body> 
                </html>
                
                ");
                ?>
                
                

                Which redirects to http://www.barnsley.co.uk if barnsley button is clicked and when returned to forwards you straight to the website aforementioned until cookie expires or is deleted by the viewer. Same principles apply to the sheffield button.

                Basically whatever value the submit button is, i.e; the text of it which is displayed on the form, is the text sent through for the redirection. I am going to be using subdomains so it fits my uses. I'm sure theres a better way of doing it but I'm not doing anymore on it, I'm sick of seeing it in fact! lol

                Php RULES!!

                Peace Out

                  Write a Reply...