Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Monday, April 4, 2011

Dynamic Site Map using PHP

the sitemap generate based on the directory structure of the site. You can easily exclude files and(or) directories.

The site map is an expandable list, similar to Windows Explorer and is accessible with or without JavaScript enabled on the user's browser. If JavaScript is not enabled, the list is simply displayed in its expanded format so all hyperlinks are visible. Directories and files are sorted alphabetical order . Note that the site map is generated from the file system, not the links contained in the pages.

this script functions by recursively calling itself to traverse the directory tree brgining at the site's root directory. It gets the file title from each file on the site and displays the page title, with a link to the file. Note that the script obtains the title of the page by extracting the text between the opening and closing title tags. That means that it only works on pages that exist (not all the variations that can be generated) and where the title is static and contained in the page.

Demo

Wednesday, March 9, 2011

Ajax login validation using php &jQuery



View Demo

In this example, first of all we’ll validate the user login detail from ajax showing the messages with some animation. If authenticated, the user will be redirected to the secure page “secure.php”. If you’ll try to directly access “secure.php”, you can’t do that.

Now let’s look at the code how to do this,

HTML Code

< form method="post" action="" id="login_form" />
  < input name="username" type="text" id="username" value="" maxlength="20" />
  < input name="password" type="password" id="password" value="" maxlength="20" />
  < input name="Submit" type="submit" id="submit" value="Login" />
< /form >

As you can see in html code, we’ve created the form with id “login_form”.

And furthermore, the CSS code is same as the one available in the pervious post of checking user availability in ajax and php.

JavaScript Code for Ajax Login validation system in PHP

First of all we need to use the jQuery library in our code,

< script src="jquery.js" type="text/javascript" language="javascript"></script >

Now let’s look at the code in javaScript to call ajax and show the animated message with fading effects.

$("#login_form").submit(function()
{
 //remove all the class add the messagebox classes and start fading
 $("#msgbox").removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);
 //check the username exists or not from ajax
 $.post("ajax_login.php",{ user_name:$('#username').val(),password:$('#password').val(),rand:Math.random() } ,function(data)
        {
   if(data=='yes') //if correct login detail
   {
  $("#msgbox").fadeTo(200,0.1,function()  //start fading the messagebox
  {
     //add message and change the class of the box and start fading
    $(this).html('Logging in.....').addClass('messageboxok').fadeTo(900,1,
                  function()
    {
           //redirect to secure page
       document.location='secure.php';
    });
  });
   }
   else
   {
    $("#msgbox").fadeTo(200,0.1,function() //start fading the messagebox
  {
    //add message and change the class of the box and start fading
    $(this).html('Your login detail sucks...').addClass('messageboxerror').fadeTo(900,1);
  });
          }
       });
       return false;//not to post the  form physically
});

As you can see above this code is preety much similar to the previous post of checking username availability in ajax and php and you can see the explanation of the above code from that post. But in above code, where the user is validated, he’ll be logged into the “secure.php” using “document.location” in JavaScript.

$("#password").blur(function()
{
 $("#login_form").trigger('submit');
});

Well, as you can see above javaScript’s code, when focus is moved away from the password it also call the for sumit action. Basically whenever you hit tab button in password field, it starts validating the user detail using ajax.

PHP Code for Ajax Login validation system

First of all lets’s look at the code of the “ajax_login.php”.

//get the posted values
$user_name=htmlspecialchars($_POST['user_name'],ENT_QUOTES);
$pass=md5($_POST['password']);
//now validating the username and password
$sql="SELECT user_name, password FROM tbl_user WHERE user_name='".$user_name."'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
//if username exists
if(mysql_num_rows($result)>0)
{
 //compare the password
 if(strcmp($row['password'],$pass)==0)
 {
  echo "yes";
  //now set the session from here if needed
  $_SESSION['u_name']=$user_name;
 }
 else
  echo "no";
}
else
 echo "no"; //Invalid Login

As you can see above, the user login detial is validated from the database. If the user login detail doesn’t exists, it just returns the “no” values and if the user login detial does exists the it just return “yes” values with setting username in session variable.

Finally, let’s look at the code of secure.php

if(empty($_SESSION['u_name']))
  header("Location:index.html");
//if logout then destroy the session and redirect the user
if(isset($_GET['logout']))
{
  session_destroy();
  header("Location:index.html");
}
echo " <a href='secure.php?logout'><b>Logout<b></a> ";
echo " <div align='center'>You Are inside secured Page</a> ";

As you can see above the code of “secure.php” is simpleforward. If the user is not authenticated by session then he’ll be redirected to “index.html”. And there is link for “logout” in this page form where user can destroy the seession.



View Demo

Download Full Source Code





Friday, December 24, 2010

syntax of php

phg  code is executed on the server, and the plain html  result is  sent to the user  browser.

php scripting  code starts with
 this is latest version only that means 5.2.9
<?php and ends with ?>

if u want excute the short tags that means old syntax <? ..?>

u modify the php.ini settings then short tags are working