cookies with snoopy class, help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

cookies with snoopy class, help

Post by visonardo »

hi, im working with snoopy class.

My problem is that when i log in into a site, all good but when i want go to other page from this site i cant, because the cookie was not created here in my pc.

i do it with snoopy thus

Code: Select all

<?

$vars="ARRAY OF VARS TO PASS like $arrayy['var']=val....";

$dom="http://www.site.com/forum/";

$snp=new snoopy;

if(count($_POST)==0)
$snp->fetch($dom);
else
$snp->submit($dom,$vars)

echo $snp->results;

?>
its almost all that i do with snoopy class, i was seening but just have to select if pass or not cookies and other var to asign the arrays cookie´s vars, but i dont know how to do it

Thank you by your help
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: cookies with snoopy class, help

Post by volka »

visonardo wrote:My problem is that when i log in into a site, all good but when i want go to other page from this site i cant, because the cookie was not created here in my pc.
I only see one http request in your script. Where does it log in?
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

i have already the form´s variables in $vars array thus

Code: Select all

$var['var'1]=val1;
$var['var2']=val2;
.....
understand?

Im doing something like a proxy, this code take pages content, if it detect that receive some cookie header must take that and send it to user that is using the proxy now.
I dont know how to take with snoopy or handly the cookie´s header is the problem. Because snoopy just have this vars to that

Code: Select all

var $cookies		=	array();		// array of cookies to pass
var $passcookies	=	true;                 // pass set cookies back through redirects
								     // NOTE: this currently does not respect
								     // dates, domains or paths.
this say the file:
$cookies is array of cookies to pass
$passcookies is true or false, determine pass set cookies back through redirects Note: this currently does not respect dates, domains or paths.


:?:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I'm not sure what you're trying to do and what your problem is.
You understand that php discards all variables, objects, resources, etc when the script is done? That includes the snoopy object and all the cookie data. If you need to store the daa between two http requests of the client you need something like e.g. sessions, see http://de2.php.net/session
Or do you want to pass the cookies the client sent to your script to another server? If so, please show your code that sets the cookie values for your snoopy object.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

volka wrote:Or do you want to pass the cookies the client sent to your script to another server? If so, please show your code that sets the cookie values for your snoopy object.

exactly it i dont know how to do, first because i dont know how to take the vars or cookie that send me the site and keep it in an array for example to then change cookies name and then read that from user pc. The problem is that i dont know how to take the cookies that suposedly the remote site would does in user´s pc.
MadScientistVX
Forum Newbie
Posts: 2
Joined: Fri Nov 03, 2006 11:13 am

Post by MadScientistVX »

I am new to this, too but I would imagine that the cookies would be passed in the header. Therefore you could try:

Code: Select all

$snoopy = new Snoopy;
$snoopy->fetch("www.example.com");
$cookies = $snoopy->headers["Set-Cookie"];
echo $cookies;
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

dont work your example

I did print_r($snoopy->headers); to see all and nothing appear in a page that request logn in.
Its a forum right? ok, when i go to this site, it something with cookies do, because appear my username and im logged already, its solved with echo $snoopy->headers["Set-Cookie"]; like this man say? i tested and didnt work, appear like if im not logged in.

I open a forum normally without use my script, by address bar. i log in in that, once time that i did it, i try open the site with my script thus:

Code: Select all

$snoopy=new Snoopy;
$snoopy->fetch($url);
echo $snoopy->headers["Set-Cookie"];
echo $snoopy->results;
MadScientistVX
Forum Newbie
Posts: 2
Joined: Fri Nov 03, 2006 11:13 am

Post by MadScientistVX »

Sorry, I messed that up a bit. Snoopy does not store it's headers in an associative array, so $snoopy->headers['setCookies'] will not work (you will need to use numbers to find your headers, i.e. $snoopy->headers[4]; or something), however print_r($snoopy->headers); should print out all the headers of the return packet. Here is my code to display returned headers:

Code: Select all

function displayHeader()
  {
  	global $snoopy;
  	
  	echo "<PRE>\nHeaders: \n";
  	print_r($snoopy->headers);
  	echo "\n</PRE>\n";
  }
Also, here is a function that reformats returned headers into an associative array:

Code: Select all

function reformHeaders($header)
  {
    $a = array();
    $cookiecnt = 0;
    foreach ($header as $key => $value)
    {
      $colpos = strpos($value, ':');
      if ($value != '\n')
      {
        if (substr($value, 0, 10) == 'Set-Cookie')
        {
          $a['Set-Cookie'][substr($value, 12, strpos($value, '=') - 12)] = substr($value, strpos($value, '=')+1);
          $cookiecnt++;
        }
        elseif ($colpos !== false)
        {
          $a[substr($value, 0, strpos($value, ':'))] = substr($value, strpos($value, ':')+2);
        }
        else
        {
          $a[$key] = $value;
        }
      }
    }
    return $a;
  }

function displayRefHeader()
  {
  	global $snoopy;
  	
  	echo "<PRE>\nReformed: \n";
  	print_r(reformHeaders($snoopy->headers));
  	echo "\n</PRE>\n";
  }
using these functions I get:
Headers:
Array
(
[0] => HTTP/1.1 302 Moved Temporarily

[1] => Server: Apache-Coyote/1.1

[2] => Set-Cookie: JSESSIONID=A0E1BF4C9B865A33354FC94AB2DC80F1; Path=/

[3] => Location: (removed here for privacy concerns)

[4] => Content-Type: text/html

[5] => Content-Length: 0

[6] => Date: Mon, 27 Nov 2006 20:34:15 GMT

)


Reformed:
Array
(
[0] => HTTP/1.1 302 Moved Temporarily

[Server] => Apache-Coyote/1.1

[Set-Cookie] => Array
(
[JSESSIONID] => A0E1BF4C9B865A33354FC94AB2DC80F1; Path=/

)

[Location] => (removed here for privacy concerns)

[Content-Type] => text/html

[Content-Length] => 0

[Date] => Mon, 27 Nov 2006 20:34:15 GMT

)
Fell free to use this code as long as it is not in a commercial product.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

thank MadScientistVX :wink:
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

other thing. for example, look this page (is any forum, but happen specially with it) REMOVED

i see it perfectly with firefox, but when i do

Code: Select all

$snp=new snoopy;

$snp->fetch("REMOVED");
echo $snp->results;
it show me 403 error

title´s page: 403 Forbidden

Code: Select all

Forbidden
You don't have permission to access /index.php on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.


why? :?
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

i dont know wich one was the problem but i fixed that
mariuspa
Forum Newbie
Posts: 1
Joined: Sat Dec 16, 2006 7:39 pm

retrieve file

Post by mariuspa »

Hello,

Has any of you any idea about how should I retrieve a file with snoopy? I mean.. by using a browser, I submit a form, then I download the file locally (the browser displays open/save/cancel dialog). I want to automate this process and I don't know how to retrieve the file. Thank you!

Best regards,
Marius Panaitescu
BobJane
Forum Newbie
Posts: 1
Joined: Sun Dec 17, 2006 3:46 am

Post by BobJane »

visonardo wrote:i dont know wich one was the problem but i fixed that
Did you try it on REMOVED ? Did it work? :lol:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.2 wrote:10. Warez, copyright violation, or promotion of any other illegal activity may NOT be linked or expressed or posted in any form.
[s]Topic Locked.[/s] Topic unlocked.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post by visonardo »

BobJane wrote:
visonardo wrote:i dont know wich one was the problem but i fixed that
Did you try it on REMOVED ? Did it work? :lol:
this has been fixed doing without snoopy class, i did that directly by myself with fsockopen
Post Reply