Basic Auth with PHP...

  
	Required Method to use :: OPTIONS ,GET OR POST

	Required PHP version : 5+: 
	
	Modeules : Not required 
	
	Server Access : Code 200

	Note :: 
			You must give exit after the headers if request method is OPTIONS
			variables used ::
 					$current_origin - to retrive ORIGIN
					$array_allowed  - set allowed site list (domain or sub domain name only with http ot https)
					$usr			- to retrive user name from user
					$pwd 			- to retrive password name from user
					$usr_base		- to set your USER name
					$pwd 			- to set your PASSWORD
					$login_successful - status of login (value : failed default , Value : success if User name and Password id correct)
					
	Important : Basic - B is caps	
	
	Browser access Type : Preflight continued to GET OR POST
		
	
	Sample Headers :
	
	Request  1
	
	**********************************************
	Request Header CORS preflight 
	**********************************************
	
	
			Key	Value
			------------------------------------------------------------
			Request	OPTIONS /xxx/xx.php HTTP/1.1
			Accept	* / *
			Origin	http://example.in
			Access-Control-Request-Method	POST
			Access-Control-Request-Headers	content-type, accept, authorization
			Accept-Encoding	gzip, deflate
			User-Agent	Mozilla/5.0 (Windows NT 6.1; Trident/7.0; EIE10;ENUSMSN; rv:11.0) like Gecko
			Host	example1.in
			Content-Length	0
			DNT	1
			Connection	Keep-Alive
			Cache-Control	no-cache
			
	**********************************************
	Response Header CORS preflight 
	**********************************************	
			
			Key	Value
			-------------------------------------------------------------
			Response	HTTP/1.1 200 OK
			Date	Wed, 24 Dec 2014 10:19:45 GMT
			Server	Apache/2.2.3 (Red Hat)
			X-Powered-By	PHP/5.1.6
			Access-Control-Allow-Credentials	true
			Access-Control-Allow-Methods	GET, POST, DELETE, PUT, OPTIONS, HEAD
			Access-Control-Allow-Headers	Authorization,Content-Type, Accept, X-Requested-With
			Access-Control-Allow-Origin	http://example.in
			Connection	close
			Content-Type	text/html; charset=UTF-8
			Content-Length	20		
	
	Request  2
	
	**********************************************
	Request Header POST
	**********************************************
	
			Key	Value
			-----------------------------------------------------------
			Request	POST /xxx/xx.php HTTP/1.1
			Accept	text/plain, * /*; q=0.01
			Content-Type	application/x-www-form-urlencoded; charset=UTF-8
			Referer	http://example1.in/yyy.php
			Accept-Language	en-US
			Origin	http://example1.in
			Accept-Encoding	gzip, deflate
			User-Agent	Mozilla/5.0 (Windows NT 6.1; Trident/7.0; EIE10;ENUSMSN; rv:11.0) like Gecko
			Host	http://example.in
			Content-Length	10
			DNT	1
			Connection	Keep-Alive
			Cache-Control	no-cache
		
		
	**********************************************
	Response Header CORS preflight 
	**********************************************		
	
		Response	HTTP/1.1 200 OK
		Date	Wed, 24 Dec 2014 10:19:45 GMT
		Server	Apache/2.2.3 (Red Hat)
		X-Powered-By	PHP/5.1.6
  
  
Example :
    
 
  

error_reporting(0);
header('Access-Control-Allow-Credentials: true'); 
header("Access-Control-Allow-Methods: GET, POST, DELETE, PUT, OPTIONS, HEAD");
header('Access-Control-Allow-Headers:Authorization,Content-Type, Accept, X-Requested-With');
$current_orgin=$_SERVER['HTTP_ORIGIN'];

$array_allowed=array("http://example.in");

if(in_array($current_orgin,$array_allowed))
{
	header("Access-Control-Allow-Origin:".$current_orgin);
}


if($_SERVER['REQUEST_METHOD']=='OPTIONS')
{

//**********************************************************************
// Blocking OPTIONS REQUEST
//**********************************************************************

exit;

}

$login_successful = 'failed';
//**********************************************************************
// CHECK USER NAME AND PASSWORD
//**********************************************************************

if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW']))
{

    $usr = $_SERVER['PHP_AUTH_USER'];
    $pwd = $_SERVER['PHP_AUTH_PW'];

	$usr_base = 'XXX';
    $pwd_base = 'XXXX';

    if ($usr == $usr_base && $pwd == $pwd_base){
        $login_successful = 'success';
		}
		else
		{
			 header('WWW-Authenticate: Basic realm="Secret page"');
				header('HTTP/1.0 401 Unauthorized');
				print "Login failed!\n";
				exit;
		}
   
}

//**********************************************************************
// CHECK IDS LOGIN IS OK
//**********************************************************************
if ($login_successful!='success'){

    // send 401 headers:
    // realm="something" will be shown in the login box 
    header('WWW-Authenticate: Basic realm="Secret page"');
    header('HTTP/1.0 401 Unauthorized');
    print "Login failed!\n";
	exit;

}

//**********************************************************************
// BEGIN YOUR CODE
//**********************************************************************
//Place you code here.............
//Success Location


  
    
    

Topics List - General

  1. How to find javascript errors?
  2. How to get the current date in java?
  3. Anna University UG PG result apr may 2015 release date
  4. How make my website in first page while Google search
  5. Connect LPG aadhar bank online using mylpg.in and rasf.uidai.gov.in, Steps to link aadhar with LPG
  6. How to create a free website?, You can't create a wesite totally free but you can create website with minimum amount
  7. How to increase speed of website
  8. PHP array Single Dimension, Multi Dimension array Declaring single and multi Dimension array, adding and deleting values in php array
  9. Basic Auth with PHP
  10. Google PageSpeed Insights tips and tricks get 99 by 100,PageSpeed Insights tips,User Experience
  11. IRCTC ticket date calculator - 120 days Advance reservation period in IRCTC
  12. IRCTC Confirm TATKAL Tickets Cancellation IRCTC Waiting List tatkal cancellation charge
  13. Best way to protect your computer from virus
  14. List of web site Help to convert File to Zip
  15. How do I get the size of a directory and Files using command line in Linux?
  16. குழந்தை வளர்ப்பில் பெற்றோர்களின் கவனத்திற்கு
  17. Health Tips necessarily have to follow !!
  18. சித்தர்கள் ஜீவ சமாதியான இடம், அவர்கள் வாழ்ந்த நாட்கள்.
  19. ஒவ்வொரு பெற்றோரும் கண்டிப்பாக படிக்க வேண்டிய பயனுள்ள விஷயங்கள்!
  20. 30 Health Tips
  21. பாய் விரித்து உறங்குவதால் கிடைக்கும் நன்மைகள் - தெரிந்துகொள்வோம்
  22. Red spots on mouth
  23. Resistor colour code
  24. Pride of our traditional rice
  25. Valaittantu properties
  26. Beetroot properties
  27. Herbal protect
  28. Small in-house treatments
  29. Where can I find my IFSC code