Jump to content

Pass value from function to cookie


rossh

Recommended Posts

Hi i have this code which takes lines in a text file puts it in an array and then prints the first array value.  Once the user refreshes the screen we move onto the next array element.

I want to put this into a function so i can use it for different text files, but i don't know how to handle the cookies and return the current $iQuoteNum to the cookie outside the function.  I've figured i can't have the setcookie function inside the function as i'm getting the headers already sent error?

Any help would be appreciated.

Thanks

Ross
            $filename = "advert.txt";
$a_advert = array();
$file = fopen($filename, "r");

while(!feof($file))
{
$aQuotes[] = fgets($file, 4096);
}

$iQuoteNum = $_COOKIE["quotes"];


if($iQuoteNum == "" || $iQuoteNum >= sizeof($aQuotes)-1)
{
$iQuoteNum = 0;
}
else
{
$iQuoteNum++;
}
setcookie ("quotes");
setcookie("quotes", $iQuoteNum);

$banner = $aQuotes[$iQuoteNum];
return $banner;
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.