Showing posts with label automatic. Show all posts
Showing posts with label automatic. Show all posts

Friday, 9 August 2013

Automatically log users into your website using HTML5 Local Storage

So your users has been registered. They have gone through the pain of logging in on a mobile phone and you want them to authenticate more easily next time round, so that they don't give up on using your web app.

The solution : HTML5 Local Storage. In the code below, I take the username and password, and put them in localstorage variables. When the (login) page is visited again, I check to see if those localstorage variables exist. If they do, I add them as values to the username and password field, and perform a submit.


<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>HTML5 Save Password</title>
<!--[if IE]>
<script src="html5.js"></script>
<script src="jquery.min.js"></script>
<form method="POST" action="set.php">
<label for"username">Username</label>
<input name="username" type="email" />
<label for"password">Password</label>
<input name="password" type="password" />
<input type="submit" />
u = false;
p = false;
if(localStorage.getItem('username') != null)
$('form input[name=username]').val(localStorage.getItem('username'));
u = true;
if(localStorage.getItem('password') != null)
$('form input[name=password]').val(localStorage.getItem('password'));
p = true;
if((u == true) && (p == true))
localStorage.username = $('form input[name=username]').val();
localStorage.password = $('form input[name=password]').val();

Friday, 13 August 2010

Automatic menu selection using PHP

Suppose I have a website. In this case a PHP driven HTML5 website, although what I'm about to explain doesn't really require HTML5 and can be easily adapted.

See demo.

I have a menu. I have a text file called menu.txt which contains my menu items like this:

index2.php,Page 2
index3.php,Page 3

And what I want to do is call a PHP script which:

  1. Reads the file.
  2. Creates the menu tags from the file contents.
  3. Automatically selects the page I'm looking at as the selected item in the menu.

I can then apply CSS as I like to the menu appearance using a different colour for the selected item.

Here is how I do it. First place the include 'navigation.php'; in your page script where you want the navigation to appear.
This is what navigation.php looks like:

/* get the contents of menu.txt */
$fileArr = file('includes/menu.txt');
/* get the name of the PHP file referred to in the URL */
$fname = basename($_SERVER['REQUEST_URI']);
/* if there isn't a PHP file extension in the URL, it must be root. Therefore call it index.php */
if(substr($fname,-4) <> ".php")
$fname = "index.php";
/* create the menu from the lines in menu.txt */
foreach ($fileArr as $line)
$menuitems = explode(",", $line);
echo '<li><a href="'.$menuitems[0].'" ';
/* Check the current pathname in menu.txt with the one in the current URL */
if($menuitems[0] == $fname)
/* If they are the same add the menuSelected class to the link */
echo 'class="menuSelected"';
echo '>'.$menuitems[1].'</a></li>';

Hey presto! Have fun.