Guest MrLeN Posted April 9, 2018 Share Posted April 9, 2018 (edited) I wrote this code. Basically it gets a user id (a referral id) from the browser and creates a 365 day cookie.However, I also have a session created because cookies don't echo onto a page the first time a page loads. So That's why session variables are in the mix.Everything works, fine. No problem. It does exactly what I want no matter if I log in or out, close the browser, start a new session .. it's all good.UNTIL .. I enter a NEW id!Then (and this happens in all browsers, so I know it's my code)..If I echo the $_SESSION[''referrer_id] it doesn't know whether it wants to display the old id or the new id (it can intermittently display either, with a mind of its own).I even added a line to END the old cookie, if a new ID is specified on line 23. But that doesn't work. It's still confused. setcookie("referrer", "", time()-3600); I am completely out of ideas. <?php session_start(); $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $current_page = preg_replace('/\?.*/', '', $current_url); $default_referrer = "1"; if (!isset($_COOKIE['referrer'])) { $user_info = get_userdata($default_referrer); $_SESSION['referrer_id'] = $user_info->user_login; $_SESSION['referrer'] = $user_info->user_email; $int = 60 * 60 * 24 * 365 + time(); setcookie("referrer",$default_referrer,time()+$int); //header('Location:' . $current_page); } $user_info = get_userdata($_COOKIE['referrer']); $_SESSION['referrer_id'] = $user_info->user_login; $_SESSION['referrer'] = $user_info->user_email; if (isset($_GET['id'])) { setcookie("referrer", "", time()-3600); $user_info = get_userdata($_GET['id']); $_SESSION['referrer_id'] = $user_info->user_login; $_SESSION['referrer'] = $user_info->user_email; $int = 60 * 60 * 24 * 365 + time(); setcookie("referrer",$_GET['id'],time()+$int); //header('Location:' . $current_page . '/?ref=' . $_SESSION['referrer_id']); } ?> Edited April 9, 2018 by MrLeN Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/ Share on other sites More sharing options...
Guest MrLeN Posted April 9, 2018 Share Posted April 9, 2018 Oh, maybe I should end the session AND the cookie! Duh....I'll try that! Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557721 Share on other sites More sharing options...
Guest MrLeN Posted April 9, 2018 Share Posted April 9, 2018 ok, I'll try this and see how I go. Thanks for your help everyone session_start(); $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $current_page = preg_replace('/\?.*/', '', $current_url); $default_referrer = "1"; if (!isset($_COOKIE['referrer'])) { $user_info = get_userdata($default_referrer); $_SESSION['referrer_id'] = $user_info->user_login; $_SESSION['referrer'] = $user_info->user_email; $int = 60 * 60 * 24 * 365 + time(); setcookie("referrer",$default_referrer,time()+$int); //header('Location:' . $current_page); } $user_info = get_userdata($_COOKIE['referrer']); $_SESSION['referrer_id'] = $user_info->user_login; $_SESSION['referrer'] = $user_info->user_email; if (isset($_GET['id'])) { setcookie("referrer", "", time()-3600); unset($_SESSION['referrer_id']); unset($_SESSION['referrer']); $user_info = get_userdata($_GET['id']); $_SESSION['referrer_id'] = $user_info->user_login; $_SESSION['referrer'] = $user_info->user_email; $int = 60 * 60 * 24 * 365 + time(); setcookie("referrer",$_GET['id'],time()+$int); //header('Location:' . $current_page . '/?ref=' . $_SESSION['referrer_id']); } Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557722 Share on other sites More sharing options...
Guest MrLeN Posted April 9, 2018 Share Posted April 9, 2018 Nope, it didn't work. It's still getting confused Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557723 Share on other sites More sharing options...
Solution kicken Posted April 9, 2018 Solution Share Posted April 9, 2018 It sounds like you have your referrer id value coming in from potentially different sources. What you need to do is decide on the priority of those sources and then check for a value in that order. $referrerId = $default_referrer; if (isset($_GET['id'])){ $referrerId = $_GET['id']; } else if (isset($_COOKIE['referrer'])){ $referrerId = $_COOKIE['referrer']; } That will assign $referrerId the value from $_GET['id'] if it exists, otherwise $_COOKIE['referrer'] if it exists, and if neither of those exist fall back to $default_referrer. After that, you know your referrer so you just do whatever you need to do with it. //Set the cookie for future page loads. $duration = 60 * 60 * 24 * 365; setcookie('referrer', $referrerId, time() + $duration); //Grab the user's details $userInfo = get_userdata($referrerId); //Do whatever with $userInfo Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557724 Share on other sites More sharing options...
Guest MrLeN Posted April 10, 2018 Share Posted April 10, 2018 Thanks KickenI have everything working now.Here's the code I finally ended with. I had a heap of unnecessary code. session_start(); $current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $current_page = preg_replace('/\?.*/', '', $current_url); $default_referrer = "1"; $int = 60 * 60 * 24 * 365 + time(); if (isset($_GET['id'])) { setcookie("referrer",$_GET['id'], time()+$int, '/');; $_COOKIE['referrer'] = $_GET['id']; } if (!isset($_COOKIE['referrer'])) { //print "<pre>"; print "We just checked cookie, it's not set.<br>"; var_dump($_COOKIE); print "</pre>"; setcookie("referrer",$default_referrer,time()+$int, '/'); $_COOKIE['referrer'] = $default_referrer; } $user_info = get_userdata($_COOKIE['referrer']); $_SESSION['referrer_id'] = $user_info->user_login; $_SESSION['referrer'] = $user_info->user_email; Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557728 Share on other sites More sharing options...
kicken Posted April 10, 2018 Share Posted April 10, 2018 You're setting your cookie expire time to something absurd by adding time() twice. Either remove it from your definition of $int or remove it from the setcookie call. Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557729 Share on other sites More sharing options...
Guest MrLeN Posted April 10, 2018 Share Posted April 10, 2018 Actually, it's set to 2067 lol.I was wondering why... Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557730 Share on other sites More sharing options...
Guest MrLeN Posted April 10, 2018 Share Posted April 10, 2018 Hey, at least my affiliates will get paid until they're 114.It's a great deal! Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557731 Share on other sites More sharing options...
dalecosp Posted April 10, 2018 Share Posted April 10, 2018 Hey, at least my affiliates will get paid until they're 114. It's a great deal! True dat ... HECK of a lot better than eBay Quote Link to comment https://forums.phpfreaks.com/topic/307081-can-you-see-anything-dodgy-in-this-code/#findComment-1557753 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.