$file='/';
session_start();
include('rsfunctions.php');
$cookieval=$_COOKIE['sessionid'];
$ip=$_SERVER['REMOTE_ADDR'];
$browser=$_SERVER['HTTP_USER_AGENT'];
if($cookieval)
{
$_SESSION['set']=0;
sqlopen('s','users',__LINE__);
if(!sqlrows("SELECT * FROM cookieinfo WHERE cookieid='".$_COOKIE['sessionid']."' AND ip='".$ip."' AND browser='".$browser."'",__LINE__))
{
setcookie('sessionid',0,1);
refresh();
}
sqlclose(__LINE__);
}
else
{
//The code in this section executes no matter what I do.
if($_SESSION['set'])
{
$_SESSION['set']=0;
die("You must have cookies set to use this website.");
}
else
{
$key="";
$pattern='ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
for($k=0;$k<36;$k++)
{
$key.=$pattern[rand(0,35)];
}
sqlopen('is','users',__LINE__);
if(!sqlrows("SELECT * FROM cookieinfo WHERE cookieid='$key'",__LINE__))
{
if(!$cookieval)
{
setcookie('sessionid',$key,time()+365.25*24*60*60);
sql("INSERT INTO cookieinfo VALUES('$key','".$ip."','".$browser."','".$lastuser."')",__LINE__); //This line inserts even if I write the function definition instead.
refresh();
}
}
sqlclose(__LINE__);
}
}
function dieerror($message,$file,$line) //void type
{
/*
Function Statement: Prints out a standard die message for mysql_query failures
1. Input
$message
formal parameter passed to function
varying part of the message
$file
formal parameter passed to function
file name of the error
$line
formal parameter passed to function
line number of the error
2. Process
Concatenate message inside die() function
3. Output
Standard message with termination of script
*/
die($message.' on '.$line.' in '.$file.': '.mysql_error());
}
function refresh()
{
/*
Function Statement: Does a silent page refresh
1. Input
$_SERVER['HTTP_HOST']
server-defined superglobal variable
domain name of the current website, or localhost
$_SERVER['REQUEST_URI']
server-defined superglobal variable
part of the website requested by the user
2. Process
Use a Location header to refresh the page
3. Output
Refreshed page
4. Assumptions
$_SERVER['HTTP_HOST'] and $_SERVER['REQUEST_URI'] are defined
*/
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
die(0);
}
function sqlclose($line) //resource type
{
/*
Function Statement: Attempts to closes an SQL connection, or prints a fail message
1. Input
$line
formal parameter passed to function
line number of the attempted close (passed via __LINE__ constant)
$file
variable passed to function via the global keyword
file name of the attempted close
$con
variable passed to function via the global keyword
connection information
2. Process
use the mysql_close() function. if not successful, print a fail message
3. Output
resource ID for the close, or error message if failed
*/
global $file;
global $con;
mysql_close($con) or dieerror("Could not close server connection",$line,$file);
}
function sql($query,$line) //resource type
{
/*
Function Statment: Process a mysql_query file or die error on fail
1. Input
$query
formal parameter passed to function
query for the sql to process
$line
formal parameter passed to function
line number of the sql call (typically __LINE__ constant)
$file
passed to function via global keyword
file name of the sql call
2. Process
call mysql_query() function, store to $result
3. Output
return $result
*/
global $file;
$result=@mysql_query($query) or dieerror("Could not process query",$file,$line);
return $result;
}
function sqldata(&$sqldata,$query,$fields,$line) //mixed type [one or two dimensional array (string or numerical types), string or numerical var]
{
global $file;
$result=sql($query,$line.' [sqldata function]');
if(is_array($fields)) //multiple columns
{
if(matchend(" LIMIT 0,1",$query)) //one row
{
while($row=mysql_fetch_assoc())
{
$sqldata[$fields[$f]]=$row[$fields[$f]];
}
}
else //multiple rows
{
for($f=0;$f<count($fields);$f++)
{
while($row=mysql_fetch_assoc())
{
$sqldata[$f][$fields]=$row[$f];
}
}
}
}
else //one column
{
if(matchend(" LIMIT 0,1",$query)) //one row
{
$row=mysql_fetch_assoc();
$sqldata=$row[$fields];
}
else //multiple rows
{
for($f=0;$f<count($fields);$f++)
{
$sqldata[$fields[$f]]=$row[$fields[$f]];
}
}
}
}
function sqlopen($user,$database,$line) //resource type
{
/*
Function Statement: Opens a connection between a user and a database //for minium permissions purposes
1. Input
$user
formal parameter passed to function
user to connect to the database
$database
formal parameter passed to function
database to select
$line
formal parameter passed to function
line number of the function call
$file
passed to function via global keyword
file name of the function call
$con
passed to function via global keyword
stores connection data
$db
passed to function via global keyword
stores database-selection data
2. Process
connect to server via mysql_connect(), store to $con. replace automatic errors with custom error
select database via mysql_select_db(), store to $db. replace automatic errors with custom error
*/
global $file;
global $con;
global $db;
$con=@mysql_connect('localhost',"phplearn_$user",'Char1133') or dieerror("Could not connect to server",$line,$file);
$db=@mysql_select_db("phplearn_reportingscience0$database",$GLOBALS['con']) or dieerror("Could not connect to database",$line,$file);
}
function sqlrows($query,$line)
{
/*
Function Statement: Show the number of rows a particular query matches
1. Input
$query
formal parameter passed to function
query for the function to process
$line
formal parameter passed to function
line number of the function call
$file
passed to function via global keyword
file name of the funciton call
2. Process
Rune sql() function and store to $result
use mysql_num_rows() on $result. replace automatic error message with custom message
*/
global $file;
$result=sql($query,$line);
$rows=@mysql_num_rows($result) or dieerror("Could not calculate rows",$file,$line);
return $rows;
}
Users browsing this forum: No registered users and 145 guests