function cgkey(&$cgkey)
{
/**
Function Statement: Generates a computer-generated key 36 characters long
1. Input
$cgkey
passed to function for return by reference
denotes computer-generated key function will return
2. Proccess
empty $cgkey just in case it was set before being passed to function
set $pattern to be all alphanumeric characters
Loop [for $p=0;$p < 36;$p++]
choose one of the letters and add to the end of $cgkey
End Loop
3. Output
pass $cgkey by reference
*/
$cgkey="";
$pattern='abcdefghijklmnopqrstuvwxyz1234567890';
for($p=0;$p<36;$p++)
{
$cgkey.=$pattern{rand(0,35)};
}
}
function closesql($line)
{
/**
Function Statement: closes the mysql connecton
1. Input
none
2. Process
close connection linked to $GLOBALS['con']
3. Output
None
*/
global $file;
mysql_close($GLOBALS['con']) or die("Could not close connection on line $line in $file." . mysql_error());
}
function opensql($user,$db,$line)
{
/**
Function Statement: connects to the database using one line of code
1. Input
$username
passed to function
denotes username to conect
$db
passed to function
denotes database to connect to
$line
passed to function via __LINE__ constant
denotes line number of any possible error
$file
grabbed via global keyword
denotes file name where any possible error may have occurred
2. Process
global $file //grabs $file variable
use $GLOBALS variable to set $con to connect to the server with the given user. quit and error on failure.
use $GLOBALS variable to set $db to select the database witht he given user. quit and error on failure.
*/
global $file;
$GLOBALS['con']=@mysql_connect("localhost","phplearn_$user","Char1133") or die("Could not connect $user to server on line $line in $file: ".mysql_error());
$GLOBALS['db']=@mysql_select_db("phplearn_reportingscience_$db") or die("Could not connect $user to $db on line $line in $file: ".mysql_error());
}
function sql($query,$line)
{
/**
Function Statement: Uses one line of code to perform an SQL query
1. Input
$query
passed to function
denotes query to process
$line
passed to function
denotes line number of failure if there is one
$file
grabbed from global scope via global keyword
denotes the file name of failure if there is one
2. Process
global $file //grabs $file from global scope
process query. quit on failure with a message saying the location. store result to $result
3. Output
reutrn $result
*/
global $file;
$result=mysql_query($query) or die("Query on $line in $file failed: " . mysql_error());
return $result;
}
function sqlrows(&$rows,$query,$line)
{
/**
Function Statement: determines the number of rows a given query has. exits and errors on failure
1. Input
$rows
passed to function for return by reference
number of rows that the query matches
$query
passed to function
denotes the query to be run
$line
passed to function via __LINE__ constant
denotes the line of any error.
$file
grabbed from global scope via global keyword
denotes the file that called this function
2. Process
global $file;
run sql function and store to $result //function definiton above
find the number of rows and store to $rows
3. Output
pass $rows by reference
*/
global $file;
$result=sql($query,$line . '(sql function)');
$rows=mysql_num_rows($result);
}
if($cookievalue)
{
opensql('s','users',__LINE__);
sqlrows($validid,"SELECT * FROM cookieinfo WHERE cookievalue='$key' AND browser='{$_SERVER['HTTP_USER_AGENT']}' AND ip='{$_SERVER['REMOTE_ADDR']}'",__LINE__);
if(!$validid)
{
setcookie('sessid','',1);
header("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
}
closesql(__LINE__);
}
while(!$_COOKIE['sessid'])
{
cgkey($key);
opensql('s','users',__LINE__);
sqlrows($isused,"SELECT * FROM cookieinfo WHERE cookievalue='$key'",__LINE__);
closesql(__LINE__);
if(!$isused)
{
opensql('i','users',__LINE__);
sql("INSERT INTO cookieinfo VALUES('A','{$_SERVER['REMOTE_ADDR']}','{$_SERVER['HTTP_USER_AGENT']}','')",__LINE__);
closesql(__LINE__);
setcookie('sessid','$key',time()+365.25*24*60*60);
header("Location: http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}");
}
}
Users browsing this forum: No registered users and 127 guests