Using javascript to read the output of the php file

Hi Expert
Situation: I have a php page on server A, it does some authentication checking, using an if else condition, I am outputting a value of 1 or 0, if authentication succeeds or not using a echo statement.
So If I call the php directly and enter credentials, I get output of 0 or 1 on the browser.

Expected : I want to read the output of that php using a javascript. The javascript function is on an html page in server B. i.e something like getContents(http://localhost:8080/login.php)

This question is very important to me at the moment.

Please help

Thanks

sandhya01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

laurent_royCommented:
here is a difference because the two pages are on different servers. The javascript function on the page loaded from server B can't access the authentication page on server B with AJAX.
You have two solutions :
-you can have a proxy on server A which will call the page on server B
-or you can use JSON with padding : http://ajaxian.com/archives/jsonp-json-with-padding
0
sandhya01Author Commented:
Hi Experts,
I am totally new to JSON, can you please help me with the code, as I only want to read '0' or '1' that will be displayed on the PHP page, by a javascript that is there on a html page on another server.

Thanks
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

laurent_royCommented:
as I said, in that case, read only '0' or '1' is not possible.

what you have to do :
on server A : a dynamic page that generates a javascript  "callback(0);" or "callback(1);", depending of authentication

on server B : an implementation of callback function, and a dynamic load of the javascript from server A.
You can simply load the script with <script type "text/javascript" src="http://serverA/authentication.php"></script> but the dynamic load is better because you can do it when you want.
// page authentication on server A
callback(1); // or callback(0);
 
/***************************/
 
//on server B
function callback(status) {
alert(status);
//do what you want;
}
 
//modify pUrl
var pUrl = "http://serverA/authentication.php";
var header = document.getElementsByTagName('head')[0];
var script = document.createElement("script");
script.setAttribute("src", pUrl);
header.appendChild(oScript);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
laurent_royCommented:
there is a mistake line17 : it's not oScript but script.
0
sandhya01Author Commented:
Hi Experts,
I am yet to implement your solutions, but believe me, to read your suggestion I have to go through a javascript guide, I will be implementing your solution tomorrow and let you know.

Also, if you have time and patience can you please explain the below things you have mentioned in your post:

1. what is "callback(0) or "callback(1)--is it a javascript function.
Do you mean that on the authentication.php page (on server A) I should write a javascript function that should generate a value of "callback(0) or "callback(1), depending on authentication?

2. will it really work: <script type "text/javascript" src="http://serverA/authentication.php"></script>? As I tried long back and it wasn't working. I think src is just to include external JS file.

3. what the following comments really does?:

//modify pUrl
var pUrl = "http://serverA/authentication.php";
var header = document.getElementsByTagName('head')[0];
var script = document.createElement("script");
script.setAttribute("src", pUrl);
header.appendChild(oScript);

Tomorrow morning 9:30 IST, I will start the execution of code and will keep you posted. But if you can provide the info on above questions, I will know what I am doing.

Thanks
0
laurent_royCommented:
1. you have to write a php page, authentication.php, that generates only one line of javascript :
 " callback(0) ; " if authentication is KO or " callback(1) ; " if authentication is OK.

2. yes it will work. You dynamically generate javascript with php. The name of the page (.php) doesn't matter. The content is text and javascript is text ! But this script markup is just a simple way to load the script. Forget it, prefer the following :

3. The pUrl parameter is the complete URL of the authentication page on server A. You have to modify it with the name of your server A.
Then the code dynamicaly creates a script markup and loads the authentication page. I prefer this way because you can write it where you want in the page, contrary to markup in 2. that will always be executed at the load of the page.

I've written a complete example. You have just to modify the pUrl parameter.
/****** SERVER A *****/
<?php
//you have to do your authentication stuff
// and generate the good line
 
echo "callback(0);";
//echo "callback(1);";
?>
 
 
 
/****** SERVER B *****/
<html>
   <head>
      <script type="text/javascript">
      
      //you have to implement this function with what you want you do
      //for example I modify a div in the body
      function callback(status) {
         //alert(status);
         var message;
         if(status==1) {
            message = "authentication OK";
         } else {
            message = "authentication KO";
         }
         //alert(message);
         document.getElementById("content").innerHTML = message;
      }
 
      //modify pUrl with the complete URL of server A
      var pUrl = "authentication.php";
      var header = document.getElementsByTagName('head')[0];
      var script = document.createElement("script");
      script.setAttribute("src", pUrl);
      header.appendChild(script);
 
      </script>
   </head>
   <body>
      <div id="content"></div>
   </body>
</html>

Open in new window

0
sandhya01Author Commented:
Hi Expert,
Things are not resolving at all: see my code below.
//authentication check on server A
login3.php
//It just calls the servlet sends it userid,password,domain and reads the content of the servlet, if servlet content is 1, the user is authenticated and echo "callback(1);" is set
 
<?php
include "HttpClient.class.php";
session_start();
 
//clear the error message
$error_msg="";
 
if(isset($_POST['btnSubmit'])){
 
//call the server at http://localhost:8080/auth-servlet/authenticate
 
$client = new HttpClient('localhost:8080');
$login2=$_REQUEST["txtUsername"]; 
$password2=$_REQUEST["txtPassword"]; 
$domain2=$_REQUEST["txtDocbase"]; 
 
if (!empty($login2) && !empty($password2) && !empty($domain2)) {
$credentials=array('login'=>$login2,'password'=>$password2,'domain'=>$domain2);
$status=$client->post('/auth-servlet/authenticate', $credentials);
if (!$status) {
    die('An error occurred: '.$client->getError());
    echo 'error';
}
else
{
$pageContents = $client->getContent();
 
if($pageContents==1)
{
//I have put the callback method as you have mentioned.
 echo "callback(1);";
 exit();
 
 //session_register("SESSION");
//$home_url = 'http://localhost:58000/authentication/redirect.php';
 // header('Location: ' . $home_url);
}
else
{
     $error_msg = 'Sorry, you must enter a valid username and password to log in.';
  }
}
}
else {
        // The username/password weren't entered so set an error message
        $error_msg = 'Sorry, you must enter your username and password to log in.';
      }
	  }
	  
?>
<html>
<head>
<SCRIPT LANGUAGE="JavaScript">
if (top.location != location) top.location.href = location.href;
</SCRIPT>
<title>FAST Index Server Administration</title>
</head>
<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" bgcolor="#2E5AA0">
<?php
  // If the session var is empty, show any error message and the log-in form; otherwise 
 
confirm the log-in
  if (empty($_SESSION['username'])) {
    echo '<p align="center" class="error" style="color: pink">' . $error_msg . '</p>';
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method=POST>
 
  <table width="450" border="0" cellpadding="0" cellspacing="0" align="center">
	<!--start table structure and top spacing - DO NOT REMOVE-->
    <tr>
      <td width="11" height="15" nowrap class="spacer">&nbsp;</td>
      <td width="10" nowrap class="spacer">&nbsp;</td>
      <td width="9" nowrap class="spacer">&nbsp;</td>
      <td class="spacer" nowrap width="99%">&nbsp;</td>
      <td width="9" nowrap class="spacer">&nbsp;</td>
      <td width="10" nowrap class="spacer">&nbsp;</td>
	<td width="11" nowrap class="spacer">&nbsp;</td>
    </tr>
	<!--end table structure and top spacing-->
    <tr>
	<td colspan="3" width="30" rowspan="3" valign="top" align="right" 
 
class="modaltopleftedge"></td>
      <td nowrap height="3">&nbsp;</td>
      <td colspan="3" rowspan="3" valign="top" align="left"></td>
    </tr>
    <tr>
      <td nowrap height="30" width="valign="top" align="center" style="border-top-style: 
 
outset; border-top-color: grey; border-top-width: 2px;" style="border-left-style: outset; 
 
border-left-color: grey; border-left-width: 2px;" style="border-right-style: outset; 
 
border-right-color: grey; border-right-width: 2px;" bgcolor='#999999'><b>FAST Index Server 
 
Administration<b></td>
    </tr>
    <tr>
       <td nowrap height="25" bgcolor='#cccccc' style="border-left-style: outset; 
 
border-left-color: grey; border-left-width: 2px;" style="border-right-style: outset; 
 
border-right-color: grey; border-right-width: 2px;">&nbsp;</td>
    </tr>
   <!--<tr>
	 <td nowrap  height="3">&nbsp;</td>
  	 <td nowrap colspan="2" >&nbsp;</td>
  	 <td nowrap >&nbsp;</td>
       <td nowrap colspan="2" >&nbsp;</td>
       <td nowrap >&nbsp;</td>
    </tr>
   <tr>
	 <td nowrap >&nbsp;</td>
	 <td nowrap colspan="2" >&nbsp;</td>
       <td nowrap >
	 start tabs or breadcrumb
	 <table cellpadding="0" cellspacing="0" border="0">
	   <tr height="25">
	     <td bgcolor="#cccccc"> </td>
	   </tr>
	  </table>
	  end tabs or breadcrumb
       </td>
	 <td nowrap colspan="2">&nbsp;</td>
       <td nowrap >&nbsp;</td>
     </tr>-->
    </table>
    <table width="450" border="0" cellpadding="0" cellspacing="0" align="center">		
    <tr>
      <td width="11" nowrap >&nbsp;</td>
	<td width="19" nowrap >&nbsp;</td>
      
	  <td nowrap width="98%" bgcolor='#FFFFF' style="border-left-style: outset; 
 
border-left-color: grey; border-left-width: 2px;" style="border-right-style: outset; 
 
border-right-color: grey; border-right-width: 2px;" >
      <div style="padding-right:10px; padding-bottom:10px; height:160px;" >
       <!--start YOUR CONTENT HERE-->
       
       <table width="100%" cellpadding="0" cellspacing="0" border="0" >
       <tr height ="20">
	     <td colspan="4">&nbsp;</td>
       </tr>
	 <tr>
         <td align="right" height="30" ><b nowrap>Login Name:</b></td>
         <td width="10">&nbsp;</td>
         <td><input type="text" name="txtUsername" value="<?php if (!empty($login2)) echo 
 
$user_username; ?>" /></td>
		 <!--<input type="text" name="txtUsername"  value=""></td>-->
         <td width="10">&nbsp;</td>               
       </tr>
	 <tr>
	   <td colspan="4" height="5"> </td>             
	 </tr>
       <tr>
         <td align="right" height="30"><b nowrap>Password:</b></td>
         <td width="10">&nbsp;</td>
         <td><input type="password" name="txtPassword"></td>
         <td width="10">&nbsp;</td>               
       </tr>
	 <!--<tr>
	   <td colspan="4" height="5"> </td>             
	 </tr>
	 <tr>
         <td align="right" height="30"><b nowrap>Domain:</b></td>
         <td width="10">&nbsp;</td>
         <td><input type="text"  name="txtDomain"  value=""></td>
         <td width="10">&nbsp;</td>               
       </tr>-->
	 <tr>
	   <td colspan="4" height="5"> </td>             
	 </tr>
	 <tr>
         <td align="right" height="30"><b nowrap>Repository:</b></td>
         <td width="10">&nbsp;</td>
         <td><input type="text" name="txtDocbase"  readonly="true" value="edms"></td>
         <td width="10">&nbsp;</td>               
       </tr>
	 <!--<tr>
	   <td colspan="4" height="5"> </td>             
	 </tr>
	 <tr>
	   <td colspan="4"></td>
	 </tr>-->
	</table>
      </div>
     
 
	
	<!--end YOUR CONTENT HERE-->
	<br>
			
   </td>
   <td nowrap width="11">&nbsp;</td>
   <td width="19" nowrap >&nbsp;</td>
   </tr>
<!--magic tags inserted by rajesh -->
   <tr align="right" height="30">
   <td>
   </td>
   <td>
   </td>
   <td align="right" style="border-left-style: outset; border-left-color: grey; 
 
border-left-width: 2px;" style="border-right-style: outset; border-right-color: grey; 
 
border-right-width: 2px;" style="border-bottom-style: outset; border-bottom-color: grey; 
 
border-bottom-width: 2px;" bgcolor='#cccccc'><input name="btnSubmit" type="submit" 
 
value="Login" ></td>
   </tr>
   
<!--magic tags inserted by rajesh end -->	
  </table>
  <table width="390" border="0" cellpadding="0" cellspacing="0" align="center">			
 
	
    <tr>
	  <td nowrap rowspan="3" valign="bottom"></td>
 
	  <td nowrap colspan="2"  height="7">&nbsp;</td>
        <td nowrap colspan="2" >&nbsp;</td>
 
	  <td nowrap colspan="2" >&nbsp;</td>
        <td nowrap rowspan="3" valign="bottom"></td>
 
    </tr>		
	<tr>
      <td nowrap >&nbsp;</td>
      <td nowrap colspan="4" align="right" >
 
         <table cellpadding="0" cellspacing="0" border="0" width="100%">
	  <tr>
	   <td align="left"><a href="Index_Agent_Help.html" target="_blank"></a></td>
	   <td class="spacer" width="40">&nbsp;</td>
	   <td align="right" nowrap>
	   <table cellpadding="0" cellspacing="0" border="0">
	     <tr>
	       <td width="5"> </td>
       
		 <td>&nbsp;</td>
		 <td></td>
		 <td>&nbsp;</td>
		 <td > </td>
		 <td  width="10">&nbsp;</td>
		 <!--td width="5" ></td>
		 <td class="button" width="5">&nbsp;</td-->
		 <!--<td><input name="btnSubmit" type="submit" value="Login"></td>-->
		
	     </tr>
	   </table>
	   </td>
	  </tr>
	</table> 
    </td>
    <td nowrap >&nbsp;</td>
   </tr>
   <tr>
	<td nowrap colspan="2"  height="7">&nbsp;</td>
      <td nowrap colspan="2" >&nbsp;</td>
	<td nowrap colspan="2" >&nbsp;</td>
   </tr>	
   <tr>
	<td nowrap ></td>
	<td nowrap colspan="2" >&nbsp;</td>
      <td nowrap colspan="2" >&nbsp;</td>
	<td nowrap colspan="2" >&nbsp;</td>
	<td nowrap ></td>
    </tr>	
    <tr>
	<td width="11" height="15" nowrap >&nbsp;</td>
      <td width="10" nowrap >&nbsp;</td>
      <td width="9" nowrap >&nbsp;</td>
      <td  nowrap width="58%">&nbsp;</td>
      <td  nowrap width="58%">&nbsp;</td>
      <td width="9" nowrap >&nbsp;</td>
      <td width="10" nowrap >&nbsp;</td>
	<td width="11" nowrap >&nbsp;</td>
    </tr>
  </table>
  </form>
  <?php
  }
  
?>
  </body></html>
 
 
 
//on server B
This html file has a javscript page that call the file login3.php, when the user enters the credential on login3.php, only the line callback(1) is displayed, why not the javascript is loading the file login3.php

Open in new window

0
sandhya01Author Commented:

//server B code
<html>
<head>
<script type="text/javascript">
function callback(status) {
if(status==1)
{
 document.cookie='isAuthenticated=true;0'
}
}
 
function getCookie(authenticated)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(authenticated + "=");
  if (c_start!=-1)
    {
    c_start=c_start + authenticated.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
 
function checkCookie()
{
 isAuthenticated=getCookie('isAuthenticated');
if(isAuthenticated==null || isAuthenticated =="" || isAuthenticated !=1)
{
 window.location="http://localhost:58000/authentication/login3.php";
}
}
 
      var pUrl = "http://localhost:58000/authentication/login3.php";
      var header = document.getElementsByTagName('head')[0];
      var script = document.createElement("script");
      script.setAttribute("src", pUrl);
      header.appendChild(script);
</script>
</head>
<body onload="checkCookie()">
<!-- BEGIN header.htm -->
<P>
 
[<A HREF="log">Log</A>]
<!--[<A HREF="/">Search</A>]-->
[<A HREF="search.htm">Search</A>]
[<A HREF="configuration">Configuration</A>]
[<A HREF="control.htm">Control</A>]
[<A HREF="statistics">Statistics</A>]
[<A HREF="blacklist">Blacklist</A>]
</P>
<HR>
<!-- END header.htm -->
</body>
</html>

Open in new window

0
sandhya01Author Commented:
Hi Expert,
I think the control in not coming back to the html/javascript page, else it should have worked, isn't it?
what is the workaround for the same.

Thx
0
sandhya01Author Commented:
Hi Experts,
Now I am checking your code:
server A:
login4.php
<?php
echo "<script>callback(1)</script>";
?>

server B
html file

<html>
   <head>
      <script type="text/javascript">
     
      //you have to implement this function with what you want you do
      //for example I modify a div in the body
      function callback(status) {
         //alert(status);
         var message;
         if(status==1) {
            message = "authentication OK";
         } else {
            message = "authentication KO";
         }
         //alert(message);
         document.getElementById("content").innerHTML = message;
      }
 
      //modify pUrl with the complete URL of server A
      var pUrl = "http://localhost:58000/authentication/login4.php";
      var header = document.getElementsByTagName('head')[0];
      var script = document.createElement("script");
      script.setAttribute("src", pUrl);
      header.appendChild(script);
 
      </script>
   </head>
   <body>
      <div id="content"></div>
        hi
   </body>
</html>

It is not working.

Please help
0
sandhya01Author Commented:
Hi
From not working I mean to say that, I am not getting any message, 'authenticatin not OK or KO' whatsoever.

Please help
0
sandhya01Author Commented:
Hi Experts,
Is it possibe to do all these things using cookies?

Thanks
0
laurent_royCommented:
why did you change what I wrote ?
in you login4.php, it's not
echo "<script>callback(1)</script>";
but
echo "callback(1)";
0
laurent_royCommented:
why using cookies ? you are in the same window so there's not use creating a cookie and reading it some lines lower...

moreover, cookies don't work on different domains, as is the case here with serverA and serverB.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.