Category Archives: PHP

PHP flat file reading and recording in sql

This example illustrates how to read a file. Txt, and insert a record into a database MSSQLSERVER.
mysql would be the same, by simply varying the user to access the database:
 

    <?php    
    //--------------------------- Read data from file  ----------------------------    
    //read from file    
    $myFile = "meteoclimatic.htm";    
    $fh = fopen($myFile, 'r');    
    $tamany=filesize($myFile);    
    $theData = fread($fh,$tamany);    
    fclose($fh);    
    //llenO las variables    
    $hora=substr( $theData ,strrpos($theData, "*UPD=") + 16, 5);    
    $any=substr( $theData ,strrpos($theData, "*UPD=") + 11, 4);    
    $mes=substr( $theData ,strrpos($theData, "*UPD=") + 8, 2);    
    $dia=substr( $theData ,strrpos($theData,  "*UPD=") + 5 ,2 );    
    $temperatura=substr( $theData ,strrpos($theData, "*TMP=") + 5, strrpos($theData, "*WND=")-(strrpos($theData, "*TMP=") + 7));    
    $pluja=substr( $theData ,strrpos($theData, "*DPCP=") + 6, strrpos($theData, "*WRUN=")-(strrpos($theData, "*DPCP=") + 8));    
    if (trim($pluja)=="") $pluja="0";    
    $humitat=substr( $theData ,strrpos($theData, "*HUM=") + 5, strrpos($theData, "*SUN=")-(strrpos($theData, "*HUM=") + 7));    
    $viento=substr( $theData ,strrpos($theData, "*WND=") + 5, strrpos($theData, "*AZI=")-(strrpos($theData, "*WND=") + 7));    
    $racha=substr( $theData ,strrpos($theData, "*WRUN=") + 6, strrpos($theData, "*MHTM=")-(strrpos($theData, "*WRUN=") + 8));    
    $maxracha=substr( $theData ,strrpos($theData, "*DGST=") + 6, strrpos($theData, "*DSUN=")-(strrpos($theData, "*DGST=") + 8));    
    $direc=substr( $theData ,strrpos($theData, "*AZI=") + 5, strrpos($theData, "*BAR=")-(strrpos($theData, "*AZI=") + 7));    
    $pressio=substr( $theData ,strrpos($theData, "*BAR=") + 5, strrpos($theData, "*HUM=")-(strrpos($theData, "*BAR=") + 7));    
    $AMDHM=$any . $mes . $dia . $hora ;    
    //--------------------- save to  slq server  ----------------------------    
    $servidor="VISTA32";    
    $usuari="sa";    
    $password="";    
    $BBDD="RegistrosEstacion";    
    $link= mssql_connect($servidor,$usuari,$password);    
    if(!$link || !mssql_select_db($BBDD, $link))    
    {    
		mssql_close($link);    
		die('Impossible abrir Base de dades!');    
    }    
    $counter=0;    
    $result=mssql_query("select * from Registros where AMDHM='" . $AMDHM . "'",$link);    
    if ($row=mssql_fetch_array($result)) {    
		mssql_close($link);    
		die('Registro ya existente !!!!');    
    }    
    $result=mssql_query("select TOP 1 PCP from Registros  where DIA='" . $dia . "' AND MES='" . $mes . "' AND ANO='" . $any ."' ORDER BY AMDHM DESC",$link);    
    if ($row=mssql_fetch_array($result)) {    
		mssql_close($link);    
		die('Registro ja existente !!!!');    
	}    
    // FI PENDENT DE FER    
    // no existe por lo tanto lo inserto !!    
    $result=mssql_query("insert into Registros (AMDHM,DIA,MES,ANO,HORA,TMP,HUM,WND,GUST,AZI,BAR,PCP) values ('" . $AMDHM . "','" . $dia . "','" . $mes . "','" . $any .  "','" . $hora . "'," . $temperatura . "," . $humitat . "," . $viento . "," . $racha . ",'" . $direc . "'," . $pressio . "," . $pluja . ")");    
    echo("insert into Registros (AMDHM,DIA,MES,ANO,HORA,TMP,HUM,WND,GUST,AZI,BAR,PCP) values ('" . $AMDHM . "','" . $dia . "','" . $mes . "','" . $any .  "','" . $hora . "'," . $temperatura . "," . $humitat . "," . $viento . "," . $racha . ",'" . $direc . "'," . $pressio . "," . $pluja . ")");    
    mssql_close($link);    
	?>

 

Facebooktwitterredditpinterestlinkedinmail

Read More ...

PHP authentication system

Here’s an example of controlling access to private pages through php, a class Authorization :

    ////////////////////////////////////////////////////////////////////    
    //Authorization class to maintain security of access to admin tool    
    class Authorization    
    {    
		public function grant()    
		{    
			$_SESSION['auth'] = true;    
			unset($_SESSION['locked']);    
			unset($_SESSION['tries']);    
		}    
		public function revoke()    
		{    
			unset($_SESSION['auth']);    
		}    
		public function fail()    
		{    
			if(!isset($_SESSION['tries']))    
				$_SESSION['tries'] = 1;    
			else    
				$_SESSION['tries']++;    
				
			if($_SESSION['tries'] &gt;= 6)    
				$_SESSION['locked'] = true;    
		}    
		public function isAuthorized()    
		{    
			return isset($_SESSION['auth']);    
		}    
		public function isLocked()    
		{    
			return isset($_SESSION['locked']);    
		}    
		public function getAttempts()    
		{    
				if(isset($_SESSION['tries']))    
					return $_SESSION['tries'];    
				else    
					return 0;    
		}    
    }
	

then at the main code of the webpage, we can put the max attemps and block the connection if you do more than this.

    session_start();    
    $maxAttempts = 3; //maximum number of password attempts    
    $password="xxx"    
         
    $auth = new Authorization(); //create authorization object    
         
    if(isset($_POST['logout'])) //user has attempted to log out    
    {    
		$auth-&gt;revoke();    
    }    
    else if(isset($_POST['login'])) //user has attempted to log in    
    {    
		if($_POST['password']==$password)    
			$auth-&gt;grant();    
		else    
			$auth-&gt;fail();    
    }    
         
    if(!$auth-&gt;isAuthorized())    
    {    
		echo "&lt;div id='loginBox'&gt;";    
		echo "&lt;h1&gt;SQLite3Admin&lt;/h1&gt;";    
		echo "&lt;h2&gt;".$DBFilename."&lt;/h2&gt;";    
		if($auth-&gt;isLocked())    
		{    
			echo "Unfortunately, you have entered an incorrect password too many times. You <span style="white-space: pre;"> </span>are locked out. Sorry.";    
		}    
		else    
		{    
			$lock = $auth-&gt;getAttempts();    
			if($lock&gt;0)    
			echo $lock." attempts out of ".$maxAttempts.".&lt;br/&gt;&lt;br/&gt;";    
			echo "&lt;form action='".$thisName."' method='post'&gt;";    
			echo "Password: &lt;input type='password' name='password'/&gt;";    
			echo "&lt;input type='submit' value='Log In' name='login'/&gt;";    
			echo "&lt;/form&gt;";    
		}    
		echo "&lt;/div&gt;";    
    }    
    
Facebooktwitterredditpinterestlinkedinmail

Read More ...

PHP Script to change pictures

In this modified example from a file of values ​​(meteoclimatic.htm) simple graphics (located in the images directory) with built-in graphics functions PHP , completing the graphics is very fast and versatile.
a link to the complete sample:
meteo.zip
The main script code:
 

Facebooktwitterredditpinterestlinkedinmail

Read More ...

Test SMTP server with PHP

This is an example of form in php to check for any mail server, to test smtp server, at the end there are  the form as would have to be designed. In my case it does not work because my hosting server can not open connections to other ports other than 80 or 443 ….

form code and mail.php:

/* * * * * * * * * * * * * * SEND EMAIL FUNCTIONS * * * * * * * * * * * * * */
//Authenticate Send - 21st March 2005
//This will send an email using auth smtp and output a log array
//logArray - connection,

function authSendEmail($smtpServer,$port,$timeout ,$auth,$username,$password ,$from, $namefrom, $to, $nameto, $subject, $message)
{
//SMTP + SERVER DETAILS
/* * * * CONFIGURATION START * * * */

$localhost = "localhost";
$newLine = "\r\n";
/* * * * CONFIGURATION END * * * * */

//Connect to the host on the specified port
$smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout);
$smtpResponse = fgets($smtpConnect, 515);
if(empty($smtpConnect))
{
$output = "Failed to connect: $smtpResponse";
return $output;
}
else
{
$logArray['connection'] = "Connected: $smtpResponse";
}
if ($auth)
{
//Request Auth Login
fputs($smtpConnect,"AUTH LOGIN" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output ."$smtpResponse";

//Send username
fputs($smtpConnect, base64_encode($username) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output . "$smtpResponse";

//Send password
fputs($smtpConnect, base64_encode($password) . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output . "$smtpResponse";
}
//Say Hello to SMTP
fputs($smtpConnect, "HELO $localhost" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output ."$smtpResponse";

//Email From
fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output . "$smtpResponse";

//Email To
fputs($smtpConnect, "RCPT TO: $to" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output ."$smtpResponse";

//The Email
fputs($smtpConnect, "DATA" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output . "$smtpResponse";

//Construct Headers
$headers = "MIME-Version: 1.0" . $newLine;
$headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;
$headers .= "To: $nameto &lt;$to&gt;" . $newLine;
$headers .= "From: $namefrom &lt;$from&gt;" . $newLine;

fputs($smtpConnect, "To: $to\nFrom: $from\nSubject: $subject\n$headers\n\n$message\n.\n");
$smtpResponse = fgets($smtpConnect, 515);
$output = $output . "$smtpResponse";

// Say Bye to SMTP
fputs($smtpConnect,"QUIT" . $newLine);
$smtpResponse = fgets($smtpConnect, 515);
$output = $output ."$smtpResponse";

return $output;
}

Facebooktwitterredditpinterestlinkedinmail

Read More ...

Categories

Subscribe to my Newsletter




By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close