www.4cnotes.info

visit xdeem.com also

4cnotes.info

4cnotes.info for Free College Notes, Placement trainng

visit www.xdeem.com also

Thanks for ure visit

Thanks for visiting our site

visit xdeem.com also.

4cnotes.info

Thank u

Showing posts with label Tips and Tricks. Show all posts
Showing posts with label Tips and Tricks. Show all posts

PHP Logout code, Kill the session or empty the session value

During the user logout time we must destroy the session, for stop any invalid user access:


sample code:

<?php
header("location:login.php");
session_start();
unset($_SESSION['admin']);
session_unset();    
session_destroy(); 
$_SESSION['admin']="out";
?> 

PHP Session Verification in Each Page


After the user succeed the login we redirect it into the success.php. if any one directly type this url mean we should ask login to see this page, for this purpose we shoul verify the session in each privilaged page with the any php file, in future if modify the session name or value mean that file will help our work simple.


sample code:
//valid.php
<?php
session_start();
if(isset($_SESSION['admin']))
echo "";//echo "Welcome ".$_SESSION['admin'];
else
header("location:index.php");
if($_SESSION['admin']=="out")
header("location:index.php");
?>

PHP Login Code with Session creation

The below code is good example of verify the Entered user login credential with the Database valid user dable, if its matching mean create the session and redirect that user to the valid page else ask to enter the valid login credential.

<?php
include("../config.php"); //Know more about config.php
$type=$_POST['type']; //HTML Text Box Value
$uname=$_POST['uname']; //HTML Text Box Value
$pwd=$_POST['pwd'];
$query=mysql_query("SELECT * FROM `adminlogin`"); // else we can directly put the user name and password in query
while($re=mysql_fetch_array($query))
{
if($uname==$re['uname'] && $pwd==$re['pwd'])
{
{
//valid user
session_start();
$_SESSION['admin']=$uname;
$flag=1;
header("location:success.php");
exit;
}
}
else
{ 
$flag=0;
}
}
if($flag==0)
{
include ("index.php");
echo "<center> </BR> <B>"."Invalid User ";
}
?>

PHP Configuration File Coding

We should save the Database configuration details in a single file (ex: config.php). After that we can include it in All our necessary PHP scripts. In case we shift the server or need to modify any credentials mean we can update that detail in a single file else we need modify all the pages where we use the server connection.


Sample Code for Database Configuration:


//config.php

<?php
$con = mysql_connect("localhost","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }


mysql_select_db("DB_Name", $con);


?>
____________________________________________


//login.php


<?php
include("config.php");
?>



How to create a Genuine user ID in (GUID) PHP


How To Generate A GUID Using PHP?


PHP has the "com_create_guid" function. Since that is a COM function one can assume it leverages internal Window's functionality to obtain a GUID.  Obviously this is useful only on IIS. Refresh the page to obtain a new GUID. A User contributed pseudo GUID generator for non-Windows servers can be found here.


What is a GUID? Excerpt from Wikipedia: "A globally unique identifier or GUID is a special type of identifier used in software applications to provide a reference number which is unique in any context (hence, "globally"), for example, in defining the internal reference for a type of access point in a software application, or for creating unique keys in a database. While each generated GUID is not guaranteed to be unique, the total number of unique keys (2128 or 3.4×1038) is so large that the probability of the same number being generated twice is extremely small."


GUIDs are useful in creating anti-CSRF tokens for web applications. 
GUID: {AE5695A7-E268-4E41-B317-E7E6AF0459C7}


Simple Use: 


<?php
$unique_php_guid = com_create_guid();
echo $unique_php_guid;
?>
sample output:
{BA04C478-4438-43CE-81ED-338F131796A4}



More then one Submit Button in Form


The following form having two submit button with two different form action




<form name="main_form" action="main.php" method="get">
<input name="one" value="text box value.">
<input type="submit" name="submit" value="first_submit" onClick="document.main_form.action='first_action.php';">
<input type="submit" name="submit" value="second_submit" onClick="document.main_form.action='second_action.php';">
<input type="submit" name="submit" value="main_submit">
</form



Textbox Auto Clear Javascript/Html


The following code is very easy code for auto clear Textbox when it focus.


First Method:
<input name="email" type="text"  class="newsform" id="email" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Enter Email To Get Updates':this.value;" value="Enter Email To Get Updates" size="28"/>


Second Method:


<input type="text"  class="login-inp" value="User Name"  onfocus="this.value=''" name="uname" />



Loading Gif Images Collection