-2

I'm googling trying to find a solution to my problem.

I need to set a cookie in a diferent file and i'm trying with curl_setopt but it doesnt work.

Any way the idea is send with PHP the value to set cookie in another page, something like this:

    file1.php

    <? //start php
    //at the begining of the file i have
session_start();
header('Content-Type: text/html; charset=utf-8');
//if i set a cookie now it give me an error cause i can not change the header
//but because i need to set a cookie now without leaving this file
//i tryed to set it in file2.php this way:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $SITE_HOME_DIR ."login.php");
// Do a POST
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'email=pok@pok.com'); 
curl_close($ch);
    ///end php
    ?>

    file2.php
    <?
//just set cookie
setcookie("TestCookie", $_POST['email'], time()+3600)
?>

But this doesn'work....

Any ideas? Thank you.

Pedro Soares
  • 623
  • 2
  • 10
  • 15
  • 1
    You need to explain your concept of "different file" or "other page". Cookies are set with [`setcookie`](http://php.net/setcookie) normally, not with [`curl_setopt`](http://php.net/curl_setopt). – mario Jan 05 '12 at 20:35
  • Still unclear what your actual goal is. What's the issue with just using `setcookie` in the first file? Explain how you expect this to behave, in which setting. (The edited code doesn't illustrate anything.) – mario Jan 05 '12 at 21:14

1 Answers1

0

You can only set a cookie for the next script with setcookie(). There is no other option. -- cURL sends distinct requests to other pages, but cannot possibly set any cookies at the client (browser).

Your error is explained in Headers already sent (reference answer). You may or may not have luck with the mentioned ob_start() workaround, if you can't/won't rewrite your page.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • You are right...i'm sorry i didn't...but i read it now. And thank you. But the funny thing is that i tryed to setcookie after a session start and a header and it is working! I hope to continue to work well. – Pedro Soares Jan 06 '12 at 00:11