Solved

POST doesn't work with CGI.pm

Posted on 2004-04-14
10
173 Views
Last Modified: 2013-12-25
I can't get POST to work when I try to use CGI-pm to do my cookie handling. Does anyone have any suggestions what I can do to work around this problem?
0
Comment
Question by:slobstar
10 Comments
 
LVL 2

Expert Comment

by:Fataqui
ID: 10827830
Hi

Can you show the code or give an example of what you you are doing, so one can see what the trouble maybe!


Fataqui!
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10827999
POST and GET methods work just fine using the CGI module.  There must be something wrong in your code.  Please show the relevant parts.
0
 
LVL 1

Author Comment

by:slobstar
ID: 10828316
Ok, what I am trying to achieve is just a regular cookie script, I've tried doing it without using any modules but it always feels like the cookies lives their own lives, sometimes they are set and sometimes they are not. If you have any good tutorial or examples of a good cookie script I would be more than glad. However, after a long time without success I wanted to try the cookie module and se if it worked better, it did, but at the same time my FORM POST's didn't work anymore, I read on the net of other people running into the same kind of problems, but I couldn't find a solution to it. But if you say it isn't so then maybe it isn't. Here is my new cookie code:

use CGI;.
my $loffe = new CGI;

sub get_cookies {
  # receive cookie
  my $AuctionWebCookieID = $loffe->cookie("AuctionWebCookieID");
 
  # if there is a cookie
  if($AuctionWebCookieID) {

    # Testa om man finns i Active Customers
    $sql_statement="SELECT cookieid FROM ACTIVECUSTOMER WHERE cookieid='$AuctionWebCookieID'";
    $sth = $dbh->query($sql_statement);
    $numrows = $sth->numrows;
 
    if($numrows<=0) {
      %temp = $sth->fetchhash;
      $sql_statement="INSERT INTO ACTIVECUSTOMER (ownerid,cookieid) VALUES ('9999999999','$AuctionWebCookieID')";
      $sth2 = $dbh->query($sql_statement);
    }
    &get_login_info;
  } else {
    &set_cookie;
  }
}

sub set_cookie{
  # unique number  
  $rnd = 0;
  for($i=0;$i<64;$i++){
    while($rnd < 48 || ($rnd > 57 && $rnd < 65) || ($rnd > 90 && $rnd < 97) || $rnd > 122){
      $rnd = rand(255);
    }
    $WebCookieID .= pack("C",$rnd);
    $rnd = 0;
  }

  # set cookie
  my $cookie_out = $loffe->cookie(-name=>"AuctionWebCookieID",-value=>"$WebCookieID");
  print $loffe->header(-cookie=>$cookie_out);
}
0
 
LVL 48

Expert Comment

by:Tintin
ID: 10828854
There's nothing in your code sample that shows where you are access POST variables.

How have you determined POST "isn't working"
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:slobstar
ID: 10830968
My bad, I used
my $loffe = new CGI;
instead of
$loffe = new CGI;
so it wasn't the POST!

but I still can't get my cookie handling script to work, it work perfect on my computer but I can't set a cookie on some of my friends computers, they hae enabled cookies but it still don't work.

# this row will work
    $cookie_out = $query->cookie(-name=> 'AuctionWebCookieID',-value=> $WebCookieID, -secure => 0);
# but what do I have to do after this row to set the cookie?
    print $query->header(-cookie=>$cookie_out);

# I've tried somethiing like this but whatever I do I can't set the cookie.
    print "Location: index.cgi\n\n";

# it will never receive the cookie with this row
$AuctionWebCookieID = $query->cookie('AuctionWebCookieID');


sub get_cookies{
  # receive cookie
  $AuctionWebCookieID = $query->cookie('AuctionWebCookieID');

  # if there is a cookie
  if($AuctionWebCookieID) {

    # Testa om man finns i Active Customers
    $sql_statement="SELECT cookieid FROM ACTIVECUSTOMER WHERE cookieid='$AuctionWebCookieID'";
    $sth = $dbh->query($sql_statement);
    $numrows = $sth->numrows;
 
    if($numrows<=0) {
      %temp = $sth->fetchhash;
      $sql_statement="INSERT INTO ACTIVECUSTOMER (ownerid,cookieid) VALUES ('9999999999','$AuctionWebCookieID')";
      $sth2 = $dbh->query($sql_statement);
    }
    &get_login_info;

  } else {
    # Get a unique ID
    $rnd = 0;
    for($i=0;$i<64;$i++){
      while($rnd < 48 || ($rnd > 57 && $rnd < 65) || ($rnd > 90 && $rnd < 97) || $rnd > 122){
        $rnd = rand(255);
    }
    $WebCookieID .= pack("C",$rnd);
    $rnd = 0;
  }

    $cookie_out = $query->cookie(-name=> 'AuctionWebCookieID',-value=> $WebCookieID, -secure => 0);
    print $query->header(-cookie=>$cookie_out);
    print "Location: index.cgi\n\n";
  }
}
0
 
LVL 1

Author Comment

by:slobstar
ID: 10831079
New information and another question?

To get the same result as on my friends computers I need to set the following preferences:

Tools->Internet Options->Privacy->Advanced options
Check  the Override automatic cookie handling box
Check the Block box

On my friends computers they don't have the Override automatic cookie handling checkbox checked, and it doesn't work for them, but it work for me when I don't have that checkbox checked.

My question:
I can't be the first person trying to do a cookie script, and I don't want my users to set different preferences to be able to use my site! What should I do?

 
0
 
LVL 1

Author Comment

by:slobstar
ID: 10831256
How can I determine if a browser can accept cookies or not?
0
 
LVL 1

Author Comment

by:slobstar
ID: 10831486
I guess I finally solved it, if I use the domain argument then it will work on medium security settings, which is default for most people.

$cookie_out = $query->cookie(-name=> 'AuctionWebCookieID',-value=> $WebCookieID,-domain=> '.mydomain.com');
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 12002696
PAQed, with points refunded (500)

Computer101
E-E Admin
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In this tutorial I will aim to show you how simple is making a small application in WhizBase, how to add, remove and update data in the DB. I will make a small address book application where you can add, browse, update and remove addresses. I wi…
Introduction This tutorial will give you a fast look what you can do with WhizBase. I expect you already know how to work with HTML at least, and that you understand the basics of the internet and how the internet works. WhizBase is a server-s…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now