Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need help with my cookies 1

Status
Not open for further replies.

Zas

Programmer
Oct 4, 2002
211
0
0
US
I'm not that much of a newbie, but I can not get my cookies working. I've tried so many different cookie scripts, and most of them aren't working for me. All I want is a cookie for $pass, and a cookie for $cname. Cname is the charecter name, and pass is the password, which is the text file that has all of their variables in there.

What I have right now, for the login is:
use CGI;
$q = new CGI;
my $cookie =
$q->cookie(
-name => "cookie_name",
-value => "cookie_contents",
);
print $q->header(-cookie => $cookie);

What I have for the next page for retrieving is:
string: my $content =
$q->cookie(-name => "cookie_name");
array: my @array =
$q->cookie(-name => "cookie_name");
hash: my %hash =
$q->cookie(-name => "cookie_name");


There is another problem-
When I open the next page, nothing shows. I took a couple of things off, and it still doesn't show. I'm sure if I can get the cookies working, than I can get the rest myself.
 
I found why the page didn't show.
The:
string: my $content =
$q->cookie(-name => "cookie_name");
array: my @array =
$q->cookie(-name => "cookie_name");
hash: my %hash =
$q->cookie(-name => "cookie_name");

or-

my $content =
$q->cookie(-name => "cookie_name");
my @array =
$q->cookie(-name => "cookie_name");
my %hash =
$q->cookie(-name => "cookie_name");

for some crazy reason blocked everything else. I still need help reading and writing the cookies. Well I appreciate it if you respond- peace.
 
try retrieving cookies like this way: $get_cookie = $q->('cookie_name');
I use it, and I don't have a problem. For sure may try ($get_cookie = $q->('cookie_name')) or die 'No such cookie';
 
ops, sorry, the correct syntax is ($get_cookie = $q->cookie('cookie_name')) or die 'no such cookie';
:)
 
Oh. I think my script is screwed. Anytime I read a cookie, it shows the rest of the page blank. Is there anything anyone can see wrong with:

(to write)

use CGI;
$q = new CGI;
my $cookie =
$q->cookie(
-name => "cnamepass",
-value => "$cname/$pass",
);
print $q->header(-cookie => $cookie);


(and to read)

($get_cookie = $q->cookie('cnamepass')) or die 'no such cookie';
open(DAT, "$cnamepass.txt");
 
try to see the value of this cookie, when created it and when retrieve it.

after $cookie = { ... };
print MYDEBUGFILE "COOKIE _VALUE: $cookie";

and see if the value is what you want. I don't now whether the value of cookie may content symbol like '/'. I think is good idea to check this.
may try this:

my $path_to_pass_file_dir = '/some/path';
my $cookie = $q->cookie(-name => 'pass', -value => $only_file_name);

my ($file = $q->cookie('pass')) or die "No cookie";
open $path_to_pass_file_dir.$file or die $!;

#without " symbols when interpolating variable. I see that if I use this for example $p = $q->param("$variable"); it don't work, but $p = $q->param($variable); is fine.
 
Let me start over. I think I have failed to explain myself.

I need $pass (password) to be the filename, and $cname (charecter name) to be the directory. This would be a lot easier if everyone would have a different password, but thats stupid. And making a new file .cgi for every username would be strange too. And I want it so you cannot easily hack into the file.

First, is it possible to download the file, or open $cname.txt from my server and see all the information stored inside?

Second, is there an easy way to save someone's information from a cgi script, so that they can enter a new page, and have the information still in there and readable... and writable?

I'll try your the other stuff gremolin.
Do you have an example you could show me somewhere though? I'll post back again if it works. I may not need any more help.

I'm sorry to be such a newb at perl.
 
Hi again:)
You know, I have to write a login system for my site too. The first I have writed before a couple of months is not good enough. I'm sorry for my English, it is not good.
So, for the new system I think to use cookies. But I don't want to write any information into a file at all. I have info for the user into a database, and I have no need to write it again. I want to create a cookie when a user is logged in. But what information I have to save into value of this cookie? The value have to be unique, no matter of her content. May be it is good decision to get the system time and PID.
my $cookie_value = $$.time; But how I can recognise who the user is? I think I can simply add a piece of information for a user into value of cookie, may be his ID from database. But there is a question - Where to put it?
My idea is: I have the time and PID and user's ID. Concat them $cookie_value = $PID.$USER_ID.time; USER_ID is at the 5-th position into this value if PID is four-digit number. And I know the user's ID is at the 5-th position (or wherever I deside to put it) when I retrieve a cookie. A problem: what if user's ID is two or tree digit length? Decision: at the end of cookie value I can add one digit which tell me what the length of user's ID is. After all of this things the result will be:

I will use a fixed position (5) for user's ID into cookie value;

For create of cookie:

my $user_id = { code which get the ID; };
my $id_length = length $user_id;
my $value = $PID.time;
$value =~ /^(\d{4})(\d+)$/;
$value = $1.$user_id.$2.$id_length;
my $cookie = $cgi->cookie(-name=>'ID', -value=>$value, -expiration=>'after_a_half_hour');
$cgi->header(-cookie=>$cookie);

And for retrieve:

($cookie = $cgi->cookie('ID')) or "user is not logged, try some action";
$cookie =~ /\d$/; #get the length, it is the last digit
$length = $1;
$cookie =~ /^\d{4}(\d{$length})\d+$/;
$user_id = $1;

And we get the user's ID back. Got the idea?
I think it will not be easy for a eventual cracker to break into this creation of my (or your) mind:)

So, what do you think about this, Zas?
 
Sorry for delay of response. It seems this website was down for a few days? Anyways it looks great. I have got my cookies working now.
My next subject will be getting the time working.
It will be interesting to learn how to make a script that updates everyone's information at the end of every hour.

Thanks for the information, it did help a lot. What is the link to your website; I might check it out some time.
Zas,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top