Jump to content

Can you see anything dodgy in this code?


Guest MrLeN
Go to solution Solved by kicken,

Recommended Posts

Guest MrLeN

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 by MrLeN
Link to comment
Share on other sites

Guest MrLeN

ok, I'll try this and see how I go. Thanks for your help everyone  :geek:  ;D  ;D 

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']);
}
Link to comment
Share on other sites

  • Solution

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
Link to comment
Share on other sites

Guest MrLeN

Thanks Kicken

I 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;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.