Develop:Custom Login Example

From FreeNATS Wiki
Jump to: navigation, search

An example custom login page. This would need to be stored in or above the web directory for the cookies to work. The includes in this example assume that the script is in your web directory (don't call it anything obvious or it will potentially get overwritten by a future FreeNATS update).

For more information see the developing with FreeNATS document.

<?php // custom-login-example.php ob_start();

// Initialise FreeNATS - assumes in the server/web directory require("include.php"); $NATS->Start();

$message="";

// Ok now our login action if (isset($_REQUEST['login']))

 {
 $login=false;
 // here we put whatever authentication code we want, in this example we use a simple if
 if ( ($_REQUEST['username']=="admin") && ($_REQUEST['password']=="wibble") ) $login=true;
 
 if ($login)
   {
   $NATS->Event("User ".$_REQUEST['username']." logged in",5,"CustomLogin","Login");
   // so ask to register the session
   // this sets the cookies etc
   $session=$NATS_Session->Register($NATS->DB,$_REQUEST['username']);
   
   if ($session===false) // failed to register - user not in FreeNATS fnuser table?
     {
     $message="Sorry, Session Failed to Register";
     $NATS->Event("Session Failed for ".$_REQUEST['username'],2,"CustomLogin","Session");
     }
   else // session registered fine so direct to wherever we want
     {
     header("Location: main.php");
     exit();
     }
   }
 else
   { // login failed
   $message="Sorry, Login Failed";
   $NATS->Event("Login Failed for ".$_REQUEST['username']." from ".$_SERVER['REMOTE_ADDR'],4,"CustomLogin","Login");
   }
 }

?> <html> <head> <title>FreeNATS Custom Login</title> </head><body>

FreeNATS Custom Login

<?php

if ($message!="") echo "

".$message."

";

?> <form action=custom-login-example.php method=post> <input type=hidden name=login value=1> Username <input type=text name=username size=30>
Password <input type=password name=password size=30>
<input type=submit value="Login to FreeNATS">
</form> </body></html>