Forum Moderators: coopster & phranque

Message Too Old, No Replies

Set Cookie

         

aliyan

6:01 pm on Oct 8, 2005 (gmt 0)

10+ Year Member



I am fresher to Perl and I need help. I have been assigned to set cookie for Username(uname)

<SCRIPT LANGUAGE="Javascript">
/* Javascript Code Here */
function chk_fields()
{
uname = document.loginfrm.uname.value;
pass = document.loginfrm.pass.value;

if (uname == "") {
alert("I Need A Username");
return false;
}

if (pass = "") {
alert("I Need A Password");
return false;
}

document.loginfrm.submit();
}
</SCRIPT>

Below is the full script please help to set the cookie for Username(uname)

#!/usr/bin/perl
push(@INC, '.');
use CGI qw(:standard);

$cgi = CGI->new;
# /* Change This Line To Your CGI Bin and Perl file */
$this = "https://www.mysite.com/cgi-bin/main.pl";

# /* Change This Line To Your www folder, without the CGI-Bin */
$fpath = "https://www.mysite.com";

# /* Change This To Your Literal Path */
$hpath = "/var/www/members/reports";

#$filepaths = "/var/www/members/reports";
$filepaths = "/var/www/members/reports";

# /* Change This To The Name Of The Password File */
$passfile = "pswd/pass.log.pl";

$fnames[0] = "inw";
$fnames[1] = "sal";

$fdesc[0] = "Inward List";
$fdesc[1] = "Sales Reports";

if (param()) {
if (param('cmd') eq "auth") {
&authenticate;
&load_type_list;
} elsif (param('fname')) {
$incookie = $cgi->cookie('buscode');
if ($incookie eq "") {
&load_login;
} else {
&do_load_links;
}
} elsif (param('getfile')) {
$incookie = $cgi->cookie('buscode');
if ($incookie eq "") {
&load_login;
} else {
&get_file;
}
}
} else {
&load_login;
}
sub get_file
{
chomp($thefile = param('getfile'));
chomp($cpath = param('path'));
`cp $cpath/$thefile $hpath`;

print header;
print <<PAGE;
<HTML>
<HEAD>
<SCRIPT LANGUAGE="Javascript">
window.location = "$fpath/$thefile";
</SCRIPT>
</HEAD>
</HTML>
PAGE
}

sub load_login
{
# /* You Can Modify Everything Below print <<PAGE;, but stop BEFORE PAGE */
print header;
print <<PAGE;
<HTML>
<HEAD>
<TITLE>MYSITE</TITLE>
<SCRIPT LANGUAGE="Javascript">
/* Javascript Code Here */
function chk_fields()
{
uname = document.loginfrm.uname.value;
pass = document.loginfrm.pass.value;

if (uname == "") {
alert("I Need A Username");
return false;
}

if (pass = "") {
alert("I Need A Password");
return false;
}

document.loginfrm.submit();
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<FORM NAME="loginfrm" METHOD="POST" ACTION="$this">
<CENTER>
<H1>Please Login</H1>
<HR><BR>
<B>Username</B><BR>
<INPUT TYPE="TEXT" NAME="uname"><BR><BR>
<B>Password</B><BR>
<INPUT TYPE="PASSWORD" NAME="pass"><BR><BR>
<INPUT TYPE="HIDDEN" NAME="cmd" VALUE="auth">
<INPUT TYPE="BUTTON" VALUE="Login" onClick="chk_fields();">
<INPUT TYPE="BUTTON" VALUE="Cancel" onClick="history.go(-1);"><BR>
</CENTER>
</FORM>
</BODY>
</HTML>
PAGE
&foot;
}
sub do_load_links
{
chomp($username = param('uname'));
chomp($password = param('pass'));
chomp($buscode = param('bcode'));
chomp($fbname = param('fname'));
$cpath = "$filepaths/$fbname";
@files = &get_file_list($cpath);

# /* You Can Modify Everything Below print <<PAGE;, but stop BEFORE PAGE */
print header;
print <<PAGE;
<HTML>
<HEAD>
<TITLE>MYSITE</TITLE>
<SCRIPT LANGUAGE="Javascript">
/* Javascript Code Here */
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<FORM NAME="linkfrm" ACTION="$this">
<CENTER>
<H1>Choose A File</H1>
<HR><BR>
PAGE
foreach $file (@files) {
$frnt = substr($file, 0, 3);
if ($buscode eq $frnt) {
print "<A HREF=\"$fpath\/$file\">$file</A><BR>\n";
`cp $cpath/$file $hpath`;
}
}
print <<PAGE1;
<BR><HR><BR>
<A HREF="#" onClick="history.go(-1);">[Go Back To The Menu]</A><BR>
</CENTER>
</FORM>
</BODY>
</HTML>
PAGE1
}
sub load_type_list
{
print <<PAGE;
<HTML>
<HEAD>
<TITLE>MYSITE</TITLE>

<style type="text/css">
.menutitle{
cursor:pointer;
margin-bottom: 5px;
background-color:#ECECFF;
color:#000000;
width:140px;
padding:2px;
text-align:center;
font-weight:bold;
/* border:1px solid #000000; */
}

.submenu{
margin-bottom: 0.5em;
}
</style>

<SCRIPT LANGUAGE="Javascript">
/* Javascript Code Here */
var persistmenu="yes";
var persisttype="sitewide";

if (document.getElementById){
document.write('<style type="text/css">\\n');
document.write('.submenu{display: none;}\\n');
document.write('</style>\\n');
}

function SwitchMenu(obj){
if(document.getElementById){
var el = document.getElementById(obj);
var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
if(el.style.display!= "block"){
for (var i=0; i<ar.length; i++){
if (ar[i].className=="submenu") {
ar[i].style.display = "none";
}
}
el.style.display = "block";
} else {
el.style.display = "none";
}
}
}
function get_cookie(Name) {
var search = Name + "=";
var returnvalue = "";
if (document.cookie.length > 0) {
offset = document.cookie.indexOf(search);
if (offset!= -1) {
offset += search.length;
end = document.cookie.indexOf(";", offset);
if (end == -1) end = document.cookie.length;
returnvalue=unescape(document.cookie.substring(offset, end));
}
}
return returnvalue;
}
function onloadfunction(){
if (persistmenu=="yes"){
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname;
var cookievalue=get_cookie(cookiename);
if (cookievalue!="") {
document.getElementById(cookievalue).style.display="block";
}
}
}
function savemenustate(){
var inc=1, blockid="";
while (document.getElementById("sub"+inc)){
if (document.getElementById("sub"+inc).style.display=="block"){
blockid="sub"+inc;
break;
}
inc++;
}
var cookiename=(persisttype=="sitewide")? "switchmenu" : window.location.pathname;
var cookievalue=(persisttype=="sitewide")? blockid+";path=/" : blockid;
document.cookie=cookiename+"="+cookievalue;
}
if (window.addEventListener) {
window.addEventListener("load", onloadfunction, false);
} else if (window.attachEvent) {
window.attachEvent("onload", onloadfunction);
} else if (document.getElementById) {
window.onload=onloadfunction;
}

if (persistmenu=="yes" && document.getElementById) {
window.onunload=savemenustate;
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="blue">
<CENTER>
<H1>MYSITE<BR><BR>
Select File Type</H1>
<HR><BR>
<div id="masterdiv">

<div class="menutitle" onclick="SwitchMenu('sub1')">Debit Reports</div>
<span class="submenu" id="sub1">
- <a href="#" onClick="document.fnamefrm.fname.value = 'inw';document.fnamefrm.submit();">Inward List</a><br>
</span>
<div class="menutitle" onclick="SwitchMenu('sub2')">Credit Reports</div>
<span class="submenu" id="sub2">
- <a href="#" onClick="document.fnamefrm.fname.value = 'sal';document.fnamefrm.submit();">Sales Reports</a><br>
</span>
<div class="menutitle" onclick="SwitchMenu('sub3')">Upload/View</div>
<span class="submenu" id="sub3">
- <a href="https://www.mysite.com/cgi-bin/uploader.cgi">Upload </a><br>
</span>
</div>

<FORM NAME="fnamefrm" METHOD="POST" ACTION="$this">
<INPUT TYPE="HIDDEN" NAME="fname" VALUE="">
<CENTER>
PAGE
print <<PAGE1;
<INPUT TYPE="HIDDEN" NAME="uname" VALUE="$username">
<INPUT TYPE="HIDDEN" NAME="pass" VALUE="$password">
<INPUT TYPE="HIDDEN" NAME="bcode" VALUE="$buscode">
</CENTER>
</FORM>
</BODY>
</HTML>
PAGE1
}
sub auth_failure
{
# /* You Can Modify Everything Below print <<PAGE;, but stop BEFORE PAGE */
print header;
print <<PAGE;
<HTML>
<HEAD>
<TITLE>MYSITE</TITLE>
<SCRIPT LANGUAGE="Javascript">
/* Javascript Code Here */
function chk_fields()
{
uname = document.loginfrm.uname.value;
pass = document.loginfrm.pass.value;

if (uname == "") {
alertrname");
return false;
}

if (pass = "") {
alert("I Need A Password");
return false;
}

document.loginfrm.submit();
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#000000" TEXT="#FFFFFF">
<FORM NAME="loginfrm" ACTION="$this">
<CENTER>
<H1>Invalid Login</H1>
<HR><BR>
<B>Username</B><BR>
<INPUT TYPE="TEXT" NAME="uname"><BR><BR>
<B>Password</B><BR>
<INPUT TYPE="PASSWORD" NAME="pass"><BR><BR>
<INPUT TYPE="HIDDEN" NAME="cmd" VALUE="auth">
<INPUT TYPE="BUTTON" VALUE="Login" onClick="chk_fields();">
<INPUT TYPE="BUTTON" VALUE="Cancel" onClick="history.go(-2);"><BR>
</CENTER>
</FORM>
</BODY>
</HTML>
PAGE
&foot;
}
sub get_file_list
{
$dirpath = shift;
system("/bin/ls -AF1 $dirpath >>ffile");
open(FH, "< ffile") or die "I suck";
while (<FH>) {
chomp($_);
$lchr = substr($_, length($_) -1);
if ($lchr ne "/") {
/(.*)/s;
push @filelist, $1;
}
}
close(FH);
unlink "ffile";

return @filelist;
}
sub authenticate
{
chomp($uname = param('uname'));
chomp($pass = param('pass'));

open(FH, "$passfile");
while (<FH>) {
chomp($_);
($tlogin, $tpasswd, $tbcode) = split(/\t/, $_);
push @logins, $tlogin;
push @passwds, $tpasswd;
push @bcodes, $tbcode;
}
close(FH);
$cnt = 0;
$flag = "false";
foreach $lname (@logins) {
if ($lname eq $uname) {
if ($passwds[$cnt] eq $pass) {
$hld = $cnt;
$flag = "true";
}
}
$cnt++;
}
if ($flag eq "false") {
# /* User Failed Authentication */
&auth_failure;
} else {
$username = $logins[$hld];
$password = $passwds[$hld];
$buscode = $bcodes[$hld];
$outcookie = $cgi->cookie(-name=>'buscode', -value=>'auth', -path=>'/');
print header(-cookie=>$outcookie);
}
}
sub foot
{
print end_html;
die;
}
sub #*$!
{
print header, start_html;
print "<CENTER>\n";
print h1("Not Enough Data"), hr, br;
print "</CENTER>\n";
print end_html;
die;
}

KevinADC

9:11 pm on Oct 8, 2005 (gmt 0)

10+ Year Member



I suggest you read the CGI.pm documentation, it will explain how to use cookies:

[perldoc.perl.org...]

PS:

your code might be removed when a moderator sees it.

aliyan

6:13 pm on Oct 9, 2005 (gmt 0)

10+ Year Member



Thanks Kevin,
I have read the article and trying to understand it.
Well its https i am getting the difficulty in getting the results. Will you please help me a little more in this so i am able to set the cookie.
Thanks in advance.

KevinADC

6:19 pm on Oct 9, 2005 (gmt 0)

10+ Year Member



if you are using a secure connection (https):


4. a "secure" flag

If the "secure" attribute is set, the cookie will only be sent to your script if the CGI request is occurring on a secure channel, such as SSL.

The interface to HTTP cookies is the cookie() method:

$cookie = cookie(-name=>'sessionID',
-value=>'xyzzy',
-expires=>'+1h',
-path=>'/cgi-bin/database',
-domain=>'.capricorn.org',
-secure=>1);
print header(-cookie=>$cookie);