PHP cookies
Hi...you guys helped me write a PHP script that logs a user in...now, i was wondering how i can mark pages that the user needs to be logged in for..i think it has something to do with cookies...i check some other sites, tried to write 'em myself, but i just couldnt figure it out...
nike_guy_man posted this at 16:28 — 12th June 2002.
They have: 840 posts
Joined: Sep 2000
This is what I use
<?php
setcookie(\"NameCookie\", \"$name\", time()+360000);
setcookie(\"loginpass\", \"$pass\", time()+360000);
header(\"Location: main.php3\");
?>
Change NameCookie to the name of the cookie you want to set the user's name to.
Change loginpass to the password you want saved (if any)
Change main.php3 to the page you want it to change to after you add the cookie, or just take that line out and make whatever you want for the rest of the page.
You can also change the length that the cookie stays, in terms of seconds, after time()
These have to be at the top of the page, as headers must be sent first.
Should work....
Let me know if it does or doesn't
kb posted this at 17:06 — 12th June 2002.
He has: 1,380 posts
Joined: Feb 2002
so i can change the NameCookie to what i want, do the variables have to match those that go with the log-in script?
what about a page that it goes to when the cookie expires, or when a visitor doesnt have the cookie...
and i can put this in the of each page i want the users to be able to access...but no one else will? how does the cookie get added?
nike_guy_man posted this at 21:18 — 12th June 2002.
They have: 840 posts
Joined: Sep 2000
That will set the cookie
To see if a cookie is there, put
<?php
if ($HTTP_COOKIE_VARS[NameCookie]) {
//page
} else {
print \"Access Denied\";
}
?>
Put the rest of the HTML where I have //page
I haven't messed with cookies in a while, as sessions work fine for me
kb posted this at 21:25 — 12th June 2002.
He has: 1,380 posts
Joined: Feb 2002
ok i'm getting confused...heres the code i have to login:
use CGI;
my $obj = new CGI;
use strict;
$| = 1;
# change only these two $vars below
# where the user database is located
my $pass_file = "/home/restricted/userdata";
# The url to redirect to with login is good
my $url = "http://1eyefilms.hypermart.net/members.html/";
# end config..........
my $pass = $obj->param('pass');
my $user = $obj->param('user');
my $send = $obj->param('send');
my $error = 0;
my $info = "";
if ($send == 1) {
if (($pass eq "")&&($user eq "")) {
$error = 3;
&show_form($error);
}
elsif (($pass ne "")&&($user eq "")) {
$error = 2;
&show_form($error);
}
elsif (($pass eq "")&&($user ne "")) {
$error = 1;
&show_form($error);
} else {
&check_pass($user,$pass);
}
} else {
&show_form($error);
}
sub check_pass {
my @users = ();
open (FILE, "$pass_file");
@users=<FILE>;
close(FILE);
my $found = 0;
foreach my $line (@users) {
my ($mname,$password,$fname,$lname,$email,$address,$city,$state,$zip,$phone1,$phone2,$phone3,$age,$agree)=split(/|/,$line);
if (($username eq $user)&&($encrypt eq $pass)) {
$found=1;
last; }
}
if ($found) {
print $obj->redirect($url);
} else {
$error = 4;
&show_form($error);
}
}
sub show_form {
if ($error == 0) {
$info = "Please Enter Your Username And Password To Login...";
}
elsif ($error == 1) {
$info = "<font color=#FF0000>The password field was left blank...</font>";
}
elsif ($error == 2) {
$info = "<font color=#FF0000>You did not enter your username...</font>";
}
elsif ($error == 3) {
$info = "Please Enter Your Username And Password To Login...";
}
elsif ($error == 4) {
$info = "<font color=#FF0000>Sorry, no user found by that name or password...</font>";
}
where do i add the add cookie thing...? (i use this as a form...not as a page)
and how would i redirect to a page if the access is denied?
(which goes in the right?)
Mark Hensler posted this at 21:30 — 12th June 2002.
He has: 4,048 posts
Joined: Aug 2000
That's a PERL script, you cannot mix PERL with PHP within the same file.
nike_guy_man posted this at 21:30 — 12th June 2002.
They have: 840 posts
Joined: Sep 2000
Whoa whoa whoa you said PHP not PERL...
Sorry my code was PHP as the title of the thread was PHP Cookies and you have something written in PERL...
kb posted this at 21:31 — 12th June 2002.
He has: 1,380 posts
Joined: Feb 2002
my fault...... ...how would a cookie be set in PERL then? can i still use the PHP to check for cookies?
nike_guy_man posted this at 02:11 — 13th June 2002.
They have: 840 posts
Joined: Sep 2000
I forget how to set cookies in PERL, but I know it is something like the PHP I had to set the cookies
If you want to check for cookies, the PHP should still work.
Try it out and see.
ROB posted this at 16:11 — 13th June 2002.
They have: 447 posts
Joined: Oct 1999
to set a cookie with Perl you need to create and send the headers yourself
print "Set-Cookie: username=$username; expires=Fri, 31-Dec-2005 00:00:00 GMT; path=/; domain=$ENV{'HTTP_HOST'};\n";
or i think CGI.PM has some cookie handling capabilities. look up CGI::Cookie
kb posted this at 14:36 — 14th June 2002.
He has: 1,380 posts
Joined: Feb 2002
ok guys, i think i get it now...thanks alot!
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.