View Single Post
  #12 (permalink)  
Old 07-12-2006, 05:15 PM
minorgod's Avatar
minorgod minorgod is offline
WebProWorld Member
 

Join Date: Jan 2005
Posts: 78
minorgod RepRank 0
Default

Sorry for this long post, but I first answered a question you DIDN'T ask before I realized what you were really asking. Then it seemed like my first answer might be somewhat helpful, so I left the first answer intact and then followed it up with the answer to the question that you really DID ask. So here goes...

Using the referrer address will probably not help you. You will need to write your links dynamically to include an identifier of some kind. You can easily pass a variable from an email to your PHP page by embedding "GET" variables in the links in your emails in the format:

http://www.somedomain.com/somepage.p...someothervalue

Everything after the "?" is a url-encoded name=value pair separated by ampersand (&) characters. Any variables passed via a link in this format will show up on your server as a "GET" variable, just like if you had submitted a form via the "GET" method. If you have a REALLY old php install, the GET variables can be accessed via the HTTP_GET_VARS global array, but most likely you have a somewhat newer installation of PHP so you can use the preferred "GET" superglobal array (though either should work). So on your php page you can output the variable value like so:

Code:
echo $HTTP_GET_VARS['somevariable'];
or

Code:
echo $_GET['somevariable'];
The first method should work for any PHP4.x install, but the second method is generally preferred. SO, using the example URL from above, either of the above code examples will output the word "value".

Knowing this, you can simply write the value of any variable passed via the URL to a hidden form field on your landing page like this:

Code:
<input type="hidden" name="somevariable" id="somevariable" value="<?php echo $_GET['somevariable']; ?>"/>
or do whatever else you want (such as inserting the value in a database). To add more variables, just remember to separate them with ampersand characters.

As one more thing to confuse you, you can access any GET or POST varialble via the "REQUEST" superglobal array which is simply a collection of all GET and POST variables, so some people prefer to use the REQUEST array if they aren't sure if a value will be submitted via GET or POST method.

Hope that helps....oh wait...I just re-read your post and now realize you were asking a totally different question! Sigh. Here's the answer the question you really asked:

Basically, you can either use the method I've outlined above to either pass the variables from page to page by embedding them in the links, or in hidden form fields. If you are submitting pages or forms via the "POST" method simply change the word "GET" in the above examples to the word "POST" and it will work the same way. HOWEVER, that's now how i'd do what you are trying to do. Personally, this seems like a perfect use for either SESSION data (if you want the server to hold the values and expire when the user closes his browser) or using COOKIES (if you want the value to be held in the user's web browser for some duration that you can specify). I prefer sessions myself so here's how to do it with sessions:

On your landing page that receives the variable via the GET method, put the following code at the top of your page but change the variables to your own:

Code:
session_start();
if(!empty($_GET['somevariable'])){
    $_SESSION['somevariable'] = $_GET['somevariable'];
}
Now on any other page where you want to retrieve that variable value, you simply put "session_start();" at the top of your page and then access the $_SESSION['somevariable'] variable like any other type of variable. The biggest complication you may run into is that you MUST start your session using session_start() before any output is sent to your client browser or it will cause a warning/error about problems sending HTTP headers, so just make sure to put it above all your html code and you should be good. Also, you must ALWAYS call the session_start() function before you will have access to any $_SESSION variables, regardless of what php page started the session or whether you happen to be on that page or not.

I mentioned using cookies as well, but this post is already too long. If you want to see examples of how to do this stuff with cookies instead of sessions, simply visit http://us3.php.net/manual/en/function.setcookie.php .
Reply With Quote