Getting started with phpViddler Part 2: User authentication and sessions
April 20th, 2008 by Colin DevroeIf you haven’t already, please be sure to read part one that Kyle put together, which covers the simple task of initiating the phpViddler class and using it to list videos. Some of the code found herein may not make sense if you haven’t read part one.
I think you’ll find, through these tutorials, that using phpViddler makes it dead simple to use Viddler’s APIs. Please leave feedback if you have any specific areas you’d like us to cover.
Authenticating a user
To authenticate a user you will need both the username and password of the user. For this example, we’ll say the username is my username, cdevroe, and the password will be a fake password of ‘password’.
There are a few important points to remember when authenticating a user using Viddler’s API, whether you use phpViddler or not. Each time you authenticate a user you are giving a sessionID representing a “session” for that username and password. If you are looking to log in a different user, you will need to re-authenticate the credentials to get another sessionID.
As we move forward with this series, you will learn more about where you use a sessionID to do things like changing meta data, uploading video, etcetera.
To start a new session, this is the code you will need to use once you have the username and password for the user.
<?php // Authenticate the user, return array of response $sessionResponse = $viddler->user_authenticate('cdevroe','password'); ?>
By default, phpViddler will return a multi-dimensional array of the Viddler.users.auth API method.
Check for errors before you do anything. Typical authentication errors are that the username does not exist (error code 101), or the password provided is incorrect (error code 103).
Once you’ve determined there are no errors, you can now put the sessionID into a variable for use later. In our next parts, we’ll be using the sessionID in a number of ways.
<?php // Store sessionID in variable. $sessionID = $sessionResponse['auth']['sessionid']; ?>
Notes about sessions
SessionIDs expire after 15 minutes of inactivity. You should build your application with this in mind. For example, if you build an uploading tool, and the upload of a video file takes longer than 15 minutes, the session you are using to upload a video with will expire before your upload is finished. Don’t panic! All you need to do is ask for a new one and you’re all set.
(Super side note: If you come to a point in your code where you need a sessionID, you can quickly run any method that requires a sessionID to see if the session is still valid, if it isn’t, get a new one, and continue your code.)
In our next part, we’ll be covering the use of our Record with Webcam API and uploading video files.
If you have any questions about authenticating users, or sessions in general, feel free to ask.
6 Responses to “Getting started with phpViddler Part...”
Kyle Slattery » Blog Archive » Getting Started with phpViddler April 21, 2008 - 7:53 pm
[...] putting together a series on how to use phpViddler, the PHP wrapper for Viddler API. Parts one and two are already up, and more are on the way, so definitely check it out if you’re interested in [...]
The Viddler Lab » Blog Archive » Getting Started With phpViddler, Part 3: Recording videos with a webcam April 22, 2008 - 5:16 am
[...] off, if you haven’t checked out part one and part two of the phpViddler series, you’ll want to do that first, as some of that code will be [...]
sajeev August 6, 2009 - 8:37 am
how to close sessionId ?
how to check it sessionid valid or not?
Chris Herdt August 12, 2009 - 5:09 pm
It looks to me like the user_authenticate() function returns array(“auth”=>array(“sessionid”=>”[session ID])) if successful, and array(“error”=>array(“code”=>”[error number]“,”description”=>”[error message]“,”details”=>”[additional detail]“)) on error.
This looks analogous to the XML returned by the Viddler API (although the API docs lack detail regarding the error response), but it might be a good idea for the phpViddler documentation to explicitly state the expected return format(s). Speaking of which, is there any documentation specific to phpViddler?
Jake Holman November 5, 2009 - 6:31 pm
Is there any way to track a videos upload progress like you do on your simple upload form?
Colin Devroe December 7, 2009 - 1:47 am
Hi Jake, we have an extension to PhpViddler called PhpViddlerUploadify that can help you to show progress of video uploads.