如何使用 cookie 从 php 发帖?

发布于 2024-11-30 03:51:58 字数 848 浏览 0 评论 0原文

如何使用 php.ini 中的 cookie 发出发布请求?

我的php代码:

<?php
$time_start = microtime(true);
$id=$_GET[id];

$data = http_build_query( array ('act' => 'load_friends_silent', 'al' => '1', 'gid'=>'0' ,'id'=>$id) );


$opts = array(
  'http'=>array(
    'header'=>"Cookie:     something=asdaefefe",
'method'=>"POST",
'content' => $data
  )
);

$context = stream_context_create($opts);
print $context;
$contents = file_get_contents('http://vkontakte.ru/al_friends.php', false, $context);

$fil=fopen("./".$id."ne.txt","w");
echo '<br><br>'.$contents;

fputs($fil,$contents);    

fclose($fil);
chmod("./".$id."ne.txt", 0777);

$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<br><br>execute time $time seconds\n";
?>

但是这个代码在服务器上不起作用; cookie 未发送。

How can I make a post request using cookies from php.

My php code:

<?php
$time_start = microtime(true);
$id=$_GET[id];

$data = http_build_query( array ('act' => 'load_friends_silent', 'al' => '1', 'gid'=>'0' ,'id'=>$id) );


$opts = array(
  'http'=>array(
    'header'=>"Cookie:     something=asdaefefe",
'method'=>"POST",
'content' => $data
  )
);

$context = stream_context_create($opts);
print $context;
$contents = file_get_contents('http://vkontakte.ru/al_friends.php', false, $context);

$fil=fopen("./".$id."ne.txt","w");
echo '<br><br>'.$contents;

fputs($fil,$contents);    

fclose($fil);
chmod("./".$id."ne.txt", 0777);

$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<br><br>execute time $time seconds\n";
?>

But this code won't work on the server; cookie not sent.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软糯酥胸 2024-12-07 03:51:58

[编辑]
要使用 PHP 发布到用户不会导航到的页面,您可以使用 CURL:

http://davidwalsh.name/execute-http-post-php-curl

在示例中,您将 $fields 设置为 cookie 值:

$fields = array(
    'lname' => urlencode($_COOKIE['last_name'] ),
    'fname' => urlencode($_COOKIE['first_name'] ),
    ...
);

[再次编辑]
根据您新发布的代码,您似乎正在尝试在您要发布到的页面上创建 cookie。首先,为其他网站创建 cookie 似乎会带来很大的安全风险,因此我 99.9% 确信这从来没有发生过,也永远不会发生。其次,PHP 不是浏览器,不能存储其他站点的 cookie...所以即使您的帖子创建了 cookie,用户的浏览器也不会拥有该 cookie。您所能做的就是发布“某事”=> urlencode('value'),然后您要发布到的页面必须在其末端创建 cookie。然而,由于 PHP 不是浏览器,因此无法保存 cookie,因此这是没有意义的。

如果你想让用户的浏览器有一个cookie,那么你需要使用像jQuery的$.post这样的东西。使用 jQuery 的 post() 方法将使用用户的浏览器转到页面并发布数据。然后,获取发布数据的页面可以在用户的​​浏览器上创建 cookie。

[阅读这些]它们可能包含一些适合您的信息。这就是你想做的吗?

在 PHP 中模拟启用 cookie 的浏览器

使用 PHP 和 PHP 模拟 POST cURL

听起来 CURL 应该能够模仿浏览器的 cookie 功能...因此您可能能够发送 cookie 数据,但正如我所说,您无法将用户设置为那些cookie 或任何东西...因为我不确定你的目的,这使得答案变得更难:

http://php.net/manual/en/function.curl-setopt.php

[EDIT]
To use PHP to post to a page that the user will NOT navigate to, you can use CURL:

http://davidwalsh.name/execute-http-post-php-curl

Where in the example, you set $fields to your cookie values:

$fields = array(
    'lname' => urlencode($_COOKIE['last_name'] ),
    'fname' => urlencode($_COOKIE['first_name'] ),
    ...
);

[EDIT AGAIN]
By your newly posted code it looks like you are trying to create a cookie on the page that you are posting to. First of all, creating cookies for other websites would seem to be a big security risk, so I'm 99.9% certain that this has never been nor ever will be possible. Secondly, PHP is not a browser and can't store cookies for other sites...so even if your post created a cookie, the user's browser would not have that cookie. All you can do is post 'something'=> urlencode('value'), and then the page you are posting to will have to create the cookie on their end. However, since PHP can't hold cookies since it's not a browser, this would be pointless.

If you want the user's browser to have a cookie, then you need to use something like jQuery's $.post. Using jQuery's post() method will use the user's browser to go to the page and post data. The page that gets the posted data can then create a cookie on the user's browser.

[Read These] they may contain some info for you. Is this what you're trying to do?

Simulating a cookie-enabled browser in PHP

Simulating a POST with PHP & cURL

It does sound like CURL should be able to mimic a browser's cookie capabilities...so you might be able to send cookie data, though, as I said, you woudln't be able to set the user to those cookies or anything...since I'm not sure of your purpose, that makes an answer harder:

http://php.net/manual/en/function.curl-setopt.php

深巷少女 2024-12-07 03:51:58

读一下这篇文章,也许会有帮助。 http://www.w3schools.com/PHP/php_cookies.asp

Read this, maybe it will help. http://www.w3schools.com/PHP/php_cookies.asp

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文