17-mysql_database/common_functions.php

Go to the documentation of this file.
00001 <?php
00013 session_register('verification_code');
00014 
00015 
00016 // This file is used to define various parameters like database name, etc.
00017 // which are used by address book for its operations 
00018 require_once('parameters.php');
00019 
00020 
00029 function get_server($variable)
00030 {
00031     if(isset($_SERVER[$variable]))
00032         return $_SERVER[$variable];
00033     else
00034         return "";
00035 }
00036 
00037 
00042 function get_remote_address()
00043 {
00044     $remote_address = get_server('REMOTE_ADDR');
00045     
00046     if($remote_address == "10.4.8.204" || $remote_address == "10.4.8.230")
00047         $remote_address = get_server('HTTP_X_FORWARDED_FOR');
00048         
00049     return $remote_address;
00050 }
00051 
00052 
00062 function get_session($variable)
00063 {
00064     if(isset($_SESSION[$variable]))
00065         return $_SESSION[$variable];
00066     else
00067         return "";
00068 }
00069 
00070 
00079 function get_post($variable)
00080 {
00081     if(isset($_POST[$variable]))
00082         return $_POST[$variable];
00083     else
00084         return "";
00085 }
00086 
00087 
00098 function create_random_image()
00099 {
00100     //Generate random number to be displayed on image
00101     $random_number = mt_rand(IMAGE_MINIMUM_NUMBER, IMAGE_MAXIMUM_NUMBER);
00102 
00103     //save number as session variable, so that it can be verified
00104     $_SESSION['verification_code']=$random_number;
00105     
00106     //Calculate width and height of image based on font size, number of
00107     //characters to be displayed and spacing
00108     $random_image_width = (imagefontwidth(IMAGE_FONT_SIZE) * IMAGE_NUMBER_OF_CHARS) + (IMAGE_SPACING * 2);
00109     $random_image_height = imagefontheight(IMAGE_FONT_SIZE) + (IMAGE_SPACING * 2);
00110     
00111     //Create image and set appropriate background color
00112     $random_image = imagecreate($random_image_width, $random_image_height);
00113     $background = imagecolorallocate($random_image, IMAGE_BACKGROUND_RED, IMAGE_BACKGROUND_GREEN, IMAGE_BACKGROUND_BLUE);
00114 
00115     //Create color for foreground digits and cuts
00116     //Using same color for both digits and cuts so that they cannot be separated
00117     //based on color, as that would defeat the purpose of adding cuts
00118     $foreground= imagecolorallocate($random_image, IMAGE_FOREGROUND_RED, IMAGE_FOREGROUND_GREEN, IMAGE_FOREGROUND_BLUE);
00119     imagestring($random_image, IMAGE_FONT_SIZE, IMAGE_SPACING, IMAGE_SPACING, $random_number, $foreground);
00120     
00121     //cut image by random lines
00122     for($counter1=0; $counter1<IMAGE_CUTS; $counter1++)
00123     {
00124         $x1 = mt_rand(1, $random_image_width);
00125         $y1 = mt_rand(1, $random_image_height);
00126         $x2 = mt_rand(1, $random_image_width);
00127         $y2 = mt_rand(1, $random_image_height);
00128         imageline($random_image, $x1, $y1, $x2, $y2, $foreground);
00129     }
00130     
00131     //save image to file    
00132     imagepng($random_image, IMAGE_FILENAME);
00133     imagedestroy($random_image);
00134 }
00135 
00136 
00146 function connect_to_database()
00147 {
00148     //connect to database server
00149     $connection = @mysql_pconnect(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD);
00150     if(!$connection)
00151     {
00152         echo "FATAL ERROR. Could not connect to database.<br> Since, ". mysql_error();
00153         exit;
00154     }
00155 
00156     //use or select database to work with
00157     $sql = "USE " . DATABASE_DBNAME;
00158     $result = mysql_query($sql, $connection);
00159     if(!$result)
00160         die("Could not use database " . DATABASE_DBNAME . " since, " . mysql_error());
00161                             
00162     return $connection;
00163 }
00164 
00165 
00171 function redirect($page)
00172 {
00173     ?>
00174         <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
00175         <html>
00176             <head>
00177                 <title>Address book redirect page</title>
00178                 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
00179                 <link rel="stylesheet" type="text/css" href="style1.css" />
00180                 <?php
00181                     echo "<meta http-equiv=\"refresh\" content=\"0;url=$page?" . session_id() . "\"/>";
00182                 ?>
00183             </head>
00184         <body>
00185         </body>
00186         </html>
00187     <?php   
00188 }
00189 
00190 ?>

Generated on Fri Nov 4 14:16:54 2011 for PHP example documentation by  doxygen 1.4.7