Current location: Hot Scripts Forums » Programming Languages » Perl » Explanation of Code... Please


Explanation of Code... Please

Reply
  #1 (permalink)  
Old 02-24-06, 09:47 PM
davidk19380 davidk19380 is offline
New Member
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Explanation of Code... Please

Here's teh scenario... we have an affiliate program. A form allows users to either apply as a new affiliate, or come back and get their html code with their affiliate number in the html code. This hasn't worked for some time, and we are finally trying to figure out why. The script is broken into 3 parts...
The first part is if a coderequest is made... that part works fine. The second part is where the problem was happening. It is for people who are applying to become an affiliate. The script is supposed to check to make sure that all the required fields are filled, then it checks to make sure the entries are valid (correct format, etc.). Once these are met, then it checks the database to see if the user already exists... blah blah blah... all of that code works correctly. The problem is the snippet of code below. This WAS placed before all the error checking and db connecting. When it was first, clicking on the "accept" button would take you directly to the "html code page" with a notice that the user already exists, and all the HTML code had blank affiliate IDs.

What is the basic function of the snippet below, and why would our script always behave as if ($ENV{HTTP_COOKIE}) is always true? As I said, if we put all the meat and potatoes programming ahead of this snippet, everything works fine (for our purpose).

Here's the snippet:

if ($ENV{HTTP_COOKIE}) {
my %cookies = &getCookies;
if ($cookies{AffiliateID}) { $clientID = $cookies{AffiliateID}; }
print "Content-type: text/html\n\n";
print "User Already Exists<br>\n";
&print_welcome;
exit;
}

can someone explain this to me?

here is the getCookies subroutine:

sub getCookies {
# cookies are seperated by a semicolon and a space, this will split
# them and return a hash of cookies
@rawCookies = split (/; /,$ENV{'HTTP_COOKIE'});

foreach(@rawCookies){
($key, $val) = split (/=/,$_);
$cookies{$key} = $val;
}

return %cookies;


Hopefully someone can give me a better explanation of what is happening here and why the script would ALWAYS run through that first snippet and then exit the script.

Thanks in advance
David
Reply With Quote
  #2 (permalink)  
Old 02-26-06, 01:50 PM
Millennium's Avatar
Millennium Millennium is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
It appears as though the script is just trying to save some time by checking if a person/user already has a cookie file that identifies them as already existing. The sub routine:

Code:
sub getCookies {
# cookies are seperated by a semicolon and a space, this will split
# them and return a hash of cookies
@rawCookies = split (/; /,$ENV{'HTTP_COOKIE'});

foreach(@rawCookies){
($key, $val) = split (/=/,$_);
$cookies{$key} = $val;
}
is just reading a cookie file stored on the users computer. The cookie file is in a specific format:

key=value;key=value;key=value

the script first splits up the cookie into an array by splitting the cookie on the semi-colon so it gets:

@rawCookies= ('key=value','key=value','key=value');

then it loops through that array and splits the key/value pairs into a hash: %cookies:

$cookies{$keys} = $value;

and return the hash back to the caller:

return %cookies;


one of those hash keys is evidently 'AffiliateID' which lets your script know the user already exists if it finds this inthe cookie file. Of course many people block cookies so the user may have to login or something else so your script knows they already exist.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Google Ad Rotation Code LaPirata Script Requests 6 12-29-10 03:31 AM
Zip Code search range errors Merovingian PHP 2 02-09-06 03:33 PM
Have a look at my JSP code which is not giving me the desired output j.gohel Everything Java 1 04-07-05 03:55 AM
Can anyone help me ? (problem using php variables in html db insert code) chronic_ PHP 2 06-13-04 11:19 AM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM


All times are GMT -5. The time now is 04:09 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.