Jump to content

Retrieve Array From Cookie And Assign To Variables


Brent Crouch

Recommended Posts

Here is a piece of php code that I use on a smarty based website.  It works fine on that site.  I'm trying to modify it to work on a non-smarty site.

 

<?php

$_wc_ab = explode(",", $_COOKIE["_wc_ab"]);

if ($_COOKIE["_wc_ab"] == "") {

  $_wc_ab = array();

  for($i=1; $i<=20; $i++) {

    $_wc_ab[] = (rand(0,1) == 0) ? "A" : "B";

  }

  setcookie("_wc_ab",join(",",$_wc_ab),time()+(60*60*24*7),"/");

}

for($i=0; $i<count($_wc_ab); $i++)

  $smarty->assign("wc_cookie_" . ($i+1), $_wc_ab[$i]);

?>

 

 

Basically, when a visitor visits the site, the script checks to see if they already have a cookie named _wc_ab.  If not, it creates the cookie and assigns it a string of 20 randomly generated As and Bs separated by commas.

 

This portion of the script is working fine on my non-smarty based site.

 

I'm having trouble figuring out the syntax of retrieving the cookie array and splitting it into 20 separate variables named wc_cookie_1, wc_cookie_2, wc_cookie_3, etc  It obviously isn't

for($i=0; $i<count($_wc_ab); $i++)

  $smarty->assign("wc_cookie_" . ($i+1), $_wc_ab[$i]);

 

since this site doesn't use smarty.

Thanks for any help anyone can give.

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.