Tag Archives: PHP

Laravel custom Authentication, With Microsoft Dynamics NAV Odata Webservice (table Customers)

I was created one project with laravel 5.4 and custom authentication to one table of Dynamics NAV (2017) trough web services, these are the steps that I was make to do this:

  • I was folow some steps As this article explains perfectly: CustomUserProvider.php 
      • Modify config/auth.php:
            'providers' => [
                'users' => [
                    //'driver' => 'eloquent',
                    'driver' => 'customuserprovider',
                    'model' => App\User::class,
                ],
        
                // 'users' => [
                //     'driver' => 'database',
                //     'table' => 'users',
                // ],
            ],
        
      • but then Modify providers/AuthServiceProvider to use my own customuserprovider:
         <?php namespace App\Providers; use Illuminate\Support\Facades\Gate; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; class AuthServiceProvider extends ServiceProvider { /** * The policy mappings for the application. * * @var array */ protected $policies = [ 'App\Model' => 'App\Policies\ModelPolicy',
            ];
        
            /**
             * Register any authentication / authorization services.
             *
             * @return void
             */
            public function boot()
            {
                $this->registerPolicies();
                // added to modify auth provider that's the trick
                 \Illuminate\Support\Facades\Auth::provider('customuserprovider', function($app, array $config) {
                return new CustomUserProvider($app['hash'], $config['model']);
                    });
                //
            }
        }
        
Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...

How to make an ajax call inside a WordPress site

After several days, finally I found how to make ajax call from my wordpress site, I hope that it will help those who have had this problem:

first, I was created a post inside a wordpress whit this html code:

<section id="main">
    <table id="tng" class="datagrid">
    	<thead><tr><th colspan="3"> 
            Sample ping App
            </th>
        </thead>
        <tbody>
    	</tr><tr>
    		<td width="50%" align="right"><b>Host or IP:</b></td>
    		<td><input  id="ip" size="20" value="" ></td>
                <td><button type="button" onclick="PING()">Ping</button></td>
    	</tr>
        </tbody>
    </table>
    <img id="image" src="spinner.gif" alt="wait..." style="display: none;">
    <br>
    
</section>	
<section id=food>
	<h3 id="notification"></h3>
</section>

As you can see the only important thing is the onclick event for the button that calls de functionPING()

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...

WordPress and WPML

Notes about WordPress and WPML:

Once installed wordpress and wpml, we can activate the translation of strings, if our subject is properly adjusted, in the files of the subject appearing chains type:


<?php _e('NIF', 'afegit'); ?>
or
<?php __('NIF', 'afegit'); ?>

in this case “Afegit” will be the domain of WPML string translation within WPML –> Themes and Plugins, we will have to check for new translations (first image), if we modify the theme files or add new fields to which we add translation functionality

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...

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);    
	?>

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

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;";    
    }    
    
Facebooktwittergoogle_plusredditpinterestlinkedinmail

Read More ...

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