carnival Posted January 4, 2010 Share Posted January 4, 2010 I have written some code for a friend that enables a site visitor to put in their affiliate code and it automatically updates all the URLs on the page to reflect the code. If the code is not set, it will display a form where they can enter the code. I prefer to have this added as a cookie because it can then be used on other pages that use the same code. My problem is that it works on one server to test it, but when I uploaded it to another server, it wont work and for the life of me, I cannot work out why. My second question is, that if I wanted to have a button that would clear the cookie if the visitor wanted to do so, how would I do this? I have tried a few ways, including overwriting the initial cookie, but it doesn't seem to work. Anyway... here is the page on one server (working) : http://www.nylonfreaks.com/test/fhgtest.php here is the SAME page on the other server (not working) : http://www.kirstyjay.com/test/fhgtest.php Here's the code : <? if(isset($affid)) { setcookie("affid",$affid); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Test</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <div> <?php $noaffid = "xxxxx"; $nyaffid = "12345"; ?> <form action="fhgtest.php" method="post"> <table> <tr> <td class="centre"> <span class="strong"> <? if(isset($affid)) { ?> <span class="strong">Your affiliate ID is : </span><span class="red"><? echo $affid ?></span> <? } else { ?> <span class="strong">Set your affiliate ID :</span> <input name="affid" type="text" size="20" maxlength="10"> <input name="Submit" type="submit" value="Submit"> <? } ?> </td> </tr> </table> </form> </div> <?php /* Set variables for particular site */ $site = "testsite"; $fhgtype = "fhg"; $fhgsite = "test"; ?> <h2>www.<?php echo($site); ?>.com</h2> <table> <tr> <td class="left"><?php /* Set FHG Number */ $fhgno = "01"; if (empty($affid)) { echo("<a href=\"http://www." . $site . ".com/" . $fhgtype . "/" . $fhgsite . "/" . $fhgno . "/index1.php?PA=" . $nyaffid . "\">http://www." . $site . ".com/" . $fhgtype . "/" . $fhgsite . "/" . $fhgno . "/index1.php?PA=" . $noaffid . "</a>"); } else { echo("<a href=\"http://www." . $site . ".com/" . $fhgtype . "/" . $fhgsite . "/" . $fhgno . "/index1.php?PA=" . $affid . "\">http://www." . $site . ".com/" . $fhgtype . "/" . $fhgsite . "/" . $fhgno . "/index1.php?PA=" . $affid . "</a>"); }; ?></td> <td class="right"><?php echo ("<a href=\"http://www." . $site . ".com/" . $fhgtype . "/" . $fhgsite . "/" . $fhgno . "/" . $fhgsite . $fhgno . ".zip\">Download Zip</a>"); ?></td> </tr> </table> </body> </html> I am obviously doing something wrong, but I cannot work out why it would be fine on one server and not another?! I have tried using the $_COOKIE function too, but I don't know if I have done so correctly. Any help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/187124-why-is-my-cookie-set-and-recall-working-on-one-server-but-not-another/ Share on other sites More sharing options...
RaythMistwalker Posted January 4, 2010 Share Posted January 4, 2010 i tihnk you must have different settings on one server than the other. I just tested on my own and not working. Quote Link to comment https://forums.phpfreaks.com/topic/187124-why-is-my-cookie-set-and-recall-working-on-one-server-but-not-another/#findComment-988188 Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2010 Share Posted January 4, 2010 I don't see any code in what you posted that is setting $affid. Where and what is $affid being set from? Quote Link to comment https://forums.phpfreaks.com/topic/187124-why-is-my-cookie-set-and-recall-working-on-one-server-but-not-another/#findComment-988197 Share on other sites More sharing options...
carnival Posted January 4, 2010 Author Share Posted January 4, 2010 affid is supposed to be being set in the form. At first affid is not set, hence the isset fonction for the cookie at start, then the affid can be set in the form. where the following code is : <span class="strong">Set your affiliate ID :</span> <input name="affid" type="text" size="20" maxlength="10"> <input name="Submit" type="submit" value="Submit"> So... what is 'supposed' to happen is that a user visits the page and as there is no affid set, it displays the form. As soon as they put their affiliate number into the form, it then sets the affid in the page and adds it to the cookie so that it updates all the links with the affid and also it is contained in the cookie so that if they visit a different page, i.e. one that contains banners or whatever, it will automatically include the affiliate code in those too, unless reset... but I haven't managed to do the reset button yet. Am I making myself clear? Sorry if not! I'm trying to be as comprehensive as poss! Quote Link to comment https://forums.phpfreaks.com/topic/187124-why-is-my-cookie-set-and-recall-working-on-one-server-but-not-another/#findComment-988204 Share on other sites More sharing options...
PFMaBiSmAd Posted January 4, 2010 Share Posted January 4, 2010 Program variables are no longer being 'magically' set from post/get/cookie/session/server/files/env variables because this allowed hackers to 'magically' set session variables to anything they wanted and a lot of sites were taken over. This feature was actually turned off by default in April of the year 2002. No new code, tutorials, books, web hosting after that point in time should have relied on this feature to magically set program variables. You need to use the correct $_POST, $_GET, $_COOKIE, $_SESSION, $_SERVER, $_FILES, or $_ENV variable where your data is actually coming from. In your case, this is from a form using the post method, so you would need to use $_POST['affid'] Quote Link to comment https://forums.phpfreaks.com/topic/187124-why-is-my-cookie-set-and-recall-working-on-one-server-but-not-another/#findComment-988206 Share on other sites More sharing options...
carnival Posted January 4, 2010 Author Share Posted January 4, 2010 Ooh! You star! I have just added : <? $affid = $_POST['affid']; if(isset($affid)) { setcookie("affid",$affid); } ?> to the beginning of the file and 'touch wood' it seems to work on the server it wasn't working on! Why are things always this simple when you've been racking your brains over them for hours? So... how would I include this as a reset? I originally tried to add a hidden for field and post a blank cookie... would this be correct? It didn't work last time, so I gave up, but maybe it will now it's working correctly.... Quote Link to comment https://forums.phpfreaks.com/topic/187124-why-is-my-cookie-set-and-recall-working-on-one-server-but-not-another/#findComment-988213 Share on other sites More sharing options...
carnival Posted January 5, 2010 Author Share Posted January 5, 2010 Right... I have finally figured this out... don't ask me how! lol. Basically, I processed the cookie setting by using a seperate 'process.php' page, which the form posted to, then used the header refresh to return to the original page : Here is process.php <? $affid = $_POST['affid']; setcookie("affid",$affid); header('Location: path/to/original/page'); ?> So... this is now solved for me, but I hope it will help someone else in the future. As you can all probably gather, I'm not a php whizz, but hopefully I'm learning! PS: Is there any way of adding 'thanks' or karma to people's profiles that tried to help? Credit where credit's due and all that... Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/187124-why-is-my-cookie-set-and-recall-working-on-one-server-but-not-another/#findComment-988529 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.