Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6742323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:49:47+00:00 2026-05-26T11:49:47+00:00

I’m using Luracast restler and i’m trying to implement some authentication by implementing iAuthenticate

  • 0

I’m using Luracast restler and i’m trying to implement some authentication by implementing iAuthenticate interface.

The thing is, my authentication code needs to query my database to retrieve the user private key. This private key will always be provided in the url request (hashed).

I wanted to open just one database connection to each request, so i need to pass the db connection variable to my class that implements iAuthenticate and to the other classes that handle all the requests. But i can’t figure out how can i pass variables to my class that implements iAuthenticate.

Is it possible?

For reference, here are the luracast examples

thks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:49:47+00:00Added an answer on May 26, 2026 at 11:49 am

    Using Single DB Connection for your API and Authentication Classes

    Create a php file called config.php and place all your db information along with db connection and selection.

    For example

    <?php
    define('DB_SERVER', 'localhost');
    define('DB_USER', 'root');
    define('DB_PASSWORD', 'password');
    define('DB_NAME', 'mysql_db');
    //initalize connection to use everywhere
    //including auth class and api classes
    mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_NAME);
    

    Include this function using require_once on both Authentication class and API class, something like (for simplicity I’m not encrypting the password here)

    <?php
    require_once 'config.php';
    class BasicAuthentication implements iAuthenticate{
        const REALM = 'Restricted API';
        public static $currentUser;
    
        function __isAuthenticated(){
            if(isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])){
                $user = $_SERVER['PHP_AUTH_USER'];
                $pass = $_SERVER['PHP_AUTH_PW'];
                $user = mysql_real_escape_string($user);
                $pass = mysql_real_escape_string($pass);
    
                mysql_query("UPDATE `login` SET logged=NOW()
                    WHERE user='$user' AND pass='$pass'");
                // echo mysql_affected_rows();
                if(mysql_affected_rows()>0){
                    self::$currentUser = $user;
                    return TRUE;
                }
            }
            header('WWW-Authenticate: Basic realm="'.self::REALM.'"');
            throw new RestException(401, 'Basic Authentication Required');
        }
    }
    

    Your API class can have a protected method that query the same db, it can be a different table that return the data using the same connection. For simplicity sake I use the same table here.

    <?php
    require_once 'config.php';
    class Simple {
        function index() {
            return 'public api result';
        }
        protected function restricted() {
            $query = mysql_query("SELECT * FROM login");
            $result = array();
            while ($row = mysql_fetch_assoc($query)) {
                $result[]=$row;
            }
            return $result;
        }
    }
    

    Using require_once makes sure that the php file is included only once on the first encounter. Even if we stop using the auth class latter our api will keep functioning

    Assuming that following SQL is used to create our db table

    --
    -- Database: `mysql_db`
    --
    
    --
    -- Table structure for table `login`
    --
    
    CREATE TABLE IF NOT EXISTS `login` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `logged` datetime DEFAULT NULL,
      `user` varchar(10) DEFAULT NULL,
      `pass` varchar(10) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
    
    --
    -- Dumping data for table `login`
    --
    
    INSERT INTO `login` (`id`, `logged`, `user`, `pass`) VALUES
    (1, '2011-11-01 22:50:05', 'arul', 'mypass'),
    (2, '2011-11-01 23:43:25', 'paulo', 'hispass');
    

    And the index.php with the following

    <?php
    require_once '../../restler/restler.php';
    
    #set autoloader
    #do not use spl_autoload_register with out parameter
    #it will disable the autoloading of formats
    spl_autoload_register('spl_autoload');
    
    $r = new Restler();
    
    $r->addAPIClass('Simple','');
    $r->addAuthenticationClass('BasicAuthentication');
    $r->handle();
    

    The Result

    if you open index.php/restricted in the browser and key in the right username and password combination, you will see the following as the result 🙂

    [
      {
        "id": "1",
        "logged": "2011-11-01 22:50:05",
        "user": "arul",
        "pass": "mypass"
      },
      {
        "id": "2",
        "logged": "2011-11-01 23:43:25",
        "user": "paulo",
        "pass": "hispass"
      }
    ]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using C# .NET 3.5 and WCF, I'm trying to write out some of the
framework is here http://luracast.com/products/restler/ i'm using restler as restful api for my work, when
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using linq2sql I'm trying to take the string in txtOilChange and update the oilChange
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using Rails 3.2.0 with haml, sass and coffeescript: Basically I am trying to disable
using Interface Builder and Storyboared, I built UITabBarController with 3 views. By default, when
I am working with Luracast Restler API framework and am wondering how to organize
Using the following as an example (with $db being a previously created database connection
Using the following database table structure: Order Table: OrderId OrderName OrderItem Table: OrderId ItemId

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.