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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using Cookies For Varification

Status
Not open for further replies.

madaxe2

Technical User
Jan 23, 2005
43
US
I can create the cookie read it back to print user name on page but i want to stop people from typing in url and skipping log in page.

So i want my html page to first check for cookie if it exists then allow people to goto any url if not to send them back to my .index page to log in.

MadAxe
 
You can do that with just a simple check for your cookie.

Code:
$q = new CGI;

my $cookie = $q->cookie( -name => "Cookie" );

if (!$cookie)
{
	print $q->redirect("./login.pl?error=1");

	exit;
}

- Rieekan
 
I think this is roughtly right where the first piece of code goes in my html then the secnd piece is the bit that gets executed to check cookie if its NOT there it redirects to login page?

MadAxe


Code:
<!--#exec cmd="/CGI_BIN/CHECK.cgi"-->

Code:
#!/Perl/bin/perl -wT


my $query = new CGI;
my $cookieinfo = $query->cookie('mcjeeves.net');


if (!$cookie)
{
    print $query->redirect("../index.html?error=1");

    exit;
}
 
Ok Found my first couple of problems

Code:
<!--#exec cmd="/CGI_BIN/CHECK.cgi"-->

should have been

<!--#exec cgi="CGI_BIN/CHECK.cgi"-->

as for the second piecwe of code STUCK

Madaxe
 
This piece of code works direct from cgi bin

when no cookie is presant however if there is a cookie presant i get a premature end of script headers error. Which iguess is being caused because i cant get it to work in conjuction with my html page


Code:
#!/Perl/bin/perl -wT

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

my $query = new CGI;
my $cookieinfo = $query->cookie('mcjeeves.net');

if (!$cookieinfo)
{
 
print $query->redirect("../index.html?error=1");

exit;

}


The Exec command works
Code:
<!--#exec cgi="CGI_BIN/CHECK.cgi"-->
with other cgi but not this one in both situations with or without cookies

the cgi code it works with is as follows:-
Code:
#!/perl/bin/perl -wT
print "Content-type: text/html\n\n";
print "<div style='position: absolute; top: 132; left: 419; width: 43; height: 25'>
<h2>Hello, world!</h2></div>\n";
i think its to do with premature end of script headers but im not sure

MADAXE
 
Your CGI code is currently only performing a function when there isn't a cookie present. you need to write some code to handle the cookie being in existence, even if that is just to exit the script.

- Rieekan
 
OK really stuck now

any chance of showing me what to do

MadAxe
 
Something like:

Code:
#!/Perl/bin/perl -wT

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

my $query = new CGI;
my $cookieinfo = $query->cookie('mcjeeves.net');

if (!$cookieinfo)
{
 
print $query->redirect("../index.html?error=1");

exit;

}
else
{
#do something here
}

- Rieekan
 
Hi Thks for the help

I guess i should have explainned my self a little better

my problem is not just how to code it but how to jump back to the web page i executed this script from.

Where you put #do something here is i guess where i need to return back to my html page. If so how do I do that. With out making this thing go round in circles.

Because at the top of my html page i have
Code:
<!--#exec cgi="CGI_BIN/CHECK.cgi"-->
which directs me to my cgi script so if i go back to the html what stops it executing the cookie check again if a cookie was found. Obviously if it is not found it directs back to my .index page.

Or do i just redirect them back to the home page that comes after my login page.

Or am i doing this all wrong should the whole page be in cgi?

MadAXE
 
Ok this works if i goto the cgi in browser with and without cookie

Code:
#!/Perl/bin/perl -wT

use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

my $query = new CGI;
my $cookieinfo = $query->cookie('mcjeeves.net');

if (!$cookieinfo)
{
 
print $query->redirect("../index.html?error=1");

exit;

}
else
{
print $query->redirect("../homepage.html?error=1");
}

However it wont exec the check cgi file with my html file but it does exec the first cgi code why wont it do the check cgi

Code:
<html>
<!--#exec cgi="CGI_BIN/CHECK.cgi"-->
<!--#exec cgi="CGI_BIN/first.cgi"-->
</html>

first cgi code attached
Code:
#!/perl/bin/perl -wT
	print "Content-type: text/html\n\n";
        print "<div style='position: absolute; top: 132; left: 419; width: 43; height: 25'>
<h2>Hello, world!</h2></div>\n";
Please Help

Madaxe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top