Jump to content

How to include JavaScript into PHP to get 2 Variables ???


sunbeam

Recommended Posts

hello 2 all. im trying for the last couple of days to get 2 simple variables/data into the MySQL Database.

Short Description of the Project:

we developed a e-learning system for our students. each student has a unique username/password to view the modules he/she should view and nothing more. since we want to give them the opportunity to run these modules from home as well, we are trying to get the USERNAME/COMPUTERNAME as well, so the students, when they sign up for the modules, they can ONLY run the modules from the home PC.

We found a very nice JavaScript, that gives us the opportunity to get these 2 values, but we are not able to retrieve these values into PHP.

The JavaScript has as follow:


[code]
//~~Author~~. Curtis Riley
//~~Email_Address~~. curril@tristategt.org
//~~Comment~~.
//Gets the current user's name, domain, and PC name, then gets the global
//groups on the domain that the user is a member of, and then checks the
//local groups on the PC to see if the user is a member. All of the data is
//displayed as collected in an Internet Explorer window controled by the
//script.

//~~Script~~.
// WSH and ADSI demo program
// Gets global and local groups
// for the current user and machine
// and displays them in IE
// Requires: ADSI 2.5, WSH 2.0,
// Scripting 5.1, IE4
//Global ActiveX objects
//Creates an instance of IE
var ie = new ActiveXObject ("InternetExplorer.Application");

//Global variables
var gsUserName, gsUserDomain, gsComputerName;
var gaGlobalGroups = new Array();
var gaLocalGroups = new Array();
var gaMembershipFrom = new Array();

// Begin Main Program

BuildOutputWindow();
ShowStatus("Getting Information...");

gsUserName = Get_UserName();
gsUserDomain = Get_UserDomain();
gsComputerName = Get_ComputerName();
ShowUserData();

gaGlobalGroups = Get_UserGlobalGroups(gsUserName, gsUserDomain);
ShowGlobalGroupData(gaGlobalGroups);

gaLocalGroups = Get_UserLocalGroups(gsUserName, gsUserDomain, gsComputerName, gaGlobalGroups);
ShowLocalGroupData(gaLocalGroups);

ShowStatus("Group information retrieved.");

// End Main Program

// ADSI and WSH Functions

// Gets the PC Name from the WSH
function Get_ComputerName(){
ShowStatus("Getting Computer Name...");
var WshNetwork = new ActiveXObject ("WScript.Network");
return (WshNetwork.ComputerName);
}
// End function GetComputerName(){

// Gets the User Name from the WSH
function Get_UserName(){
ShowStatus("Getting User Name...");
var WshNetwork = new ActiveXObject ("WScript.Network");
return (WshNetwork.UserName);
}
// End function GetUserName(){

// Gets the Domain Name from the WSH
function Get_UserDomain(){
ShowStatus("Getting Domain Name...");
var WshNetwork = new ActiveXObject ("WScript.Network");
return (WshNetwork.UserDomain);
}
// End function Get_UserDomain(){

// Gets the global groups the user is a member of from ADSI
function Get_UserGlobalGroups(sUserName, sUserDomain){
ShowStatus("Getting User Global Group Membership...");
var oDomain; //Will hold the NT domain object
var eDomain; //an Enumerator that points to oDomain
var oGroup; //Will hold a group object
var i; //Counter variable

var VB_array; //a Dictionary object used to create a VBArray
var VBGroupFilter; //will hold the value "Group" in a VBArray

var aGroups = new Array(); //Holds the groups the user is a member of

// Create a VBarray to hold the filter value (yes, it is a kludge)
VB_array = new ActiveXObject("Scripting.Dictionary");
VB_array.add("Group", "");
VBGroupFilter = VB_array.Keys();

// Get the IADsContainer object for the domain
oDomain = GetObject("WinNT://" + sUserDomain);

//Filter the IADsContainer so that only objects that
//are groups show up when looking at it
oDomain.Filter = VBGroupFilter;

//Create an enumerator object so that we can step through all the groups
//in the domain
eDomain = new Enumerator(oDomain)

i = 0; //Initialize counter to step through array
for (;!eDomain.atEnd();eDomain.moveNext()){
//Since oDomain is filtered by group, each eDomain item is a group
oGroup = eDomain.item();
ShowStatus("Checking membership in group " + oGroup.Name + "....");
//Use the IsMember() method to find out if user is in the group

if ( oGroup.IsMember("WinNT://" + sUserDomain + "/" + sUserName) ){
//Store the group in the array, increment array counter
aGroups[i++] = oGroup;
//Show the groups as they come up
ShowGlobalGroupData(aGroups);
}
}
ShowStatus("Done getting global group membership");
return (aGroups);
}
// function Get_UserGlobalGroups(){

// Gets the local groups the user is a member of

function Get_UserLocalGroups(sUserName, sUserDomain, sComputerName, aGlobalGroups){
ShowStatus("Getting User Local Group Membership (may take a while)...");
var oDomain; //Will hold the NT local domain object
var eDomain; //an Enumerator that points to oDomain
var oGroup; //Will hold a group object
var i; //Counter variable

var VB_array; //a Dictionary object used to create a VBArray
var VBGroupFilter; //will hold the value "Group" in a VBArray

var aGroups = new Array(); //Holds the groups the user is a member of

// Create a VBarray to hold the filter value (yes, it is a kludge)
VB_array = new ActiveXObject("Scripting.Dictionary");
VB_array.add("Group", "");
VBGroupFilter = VB_array.Keys();

// Get the IADsContainer object for the computer
oDomain = GetObject("WinNT://" + sComputerName);

//Filter the IADsContainer so that only objects that
//are groups show up when looking at it
oDomain.Filter = VBGroupFilter;

//Create an enumerator object so that we can step through all the groups
//in the local domain
eDomain = new Enumerator(oDomain)

//Use a for loop to step through the enumerator and look at all the groups
i = 0; //Initialize counter to step through array
for (;!eDomain.atEnd();eDomain.moveNext()){
//Since oDomain is filtered by group, each eDomain item is a group
oGroup = eDomain.item();
ShowStatus("Checking membership in group " + oGroup.Name + "....");
//Use the IsMember() method to find out if user is in the group
if ( oGroup.IsMember("WinNT://" + sUserDomain + "/" + sUserName) ){
//Add a new property to indicate where the membership comes from
gaMembershipFrom[i] = sUserName;
//Store the group in the array, increment array counter
aGroups[i++] = oGroup;
//Show the groups as they come up
ShowLocalGroupData(aGroups);
}
//Now loop through all global groups that the user is a member of
//and check to see if those global groups belong to the local group
//Necessary since the .IsMember method doesn't check to see if a
//a user is in the global groups that are members of the local group
for (j =0; j < aGlobalGroups.length; j++){
ShowStatus("Checking membership in group " + oGroup.Name + "....");
if ( oGroup.IsMember("WinNT://" + sUserDomain + "/" + aGlobalGroups[j].Name) ){
//Now check against the last local group added to list to see if
//the user is already a member to prevent adding duplicate names
//since (i) was incremented, need to use (i-1) to get current group
if (aGroups[i-1].Name != oGroup.Name){
gaMembershipFrom[i] = aGlobalGroups[j].Name;
aGroups[i++] = oGroup;
} else {
//Add current global group name to the MembershipFrom list
gaMembershipFrom[i-1] += "<br>" + aGlobalGroups[j].Name;
}
ShowLocalGroupData(aGroups);
}
}
}
ShowStatus("Done getting local group membership");
return (aGroups);
} // End function Get_UserLocalGroups(){

// IE and display Functions

// Creates the IE window
// Require the object ie.
function BuildOutputWindow(){
var n = 0; //dummy variable while waiting for IE to start
var s = ""; //String that holds the HTML to build window

//Set window properties
ie.height = 480;
ie.width = 640;
ie.menubar = false;
ie.toolbar = false;
ie.statusbar = false;
ie.addressbar = false;
ie.resizable = true;
ie.navigate ("about:blank");

//Loop while IE is opening
while (ie.busy) {++n};

ie.document.body.innerHTML = "Building Document..." + "<br>load time= " + n;
ie.visible = true;

//Start building document. Each element is given an ID for DHTML use
//Single quotes are used to prevent having to escape double quotes
//Newlines are thrown in to make it easier to read if sent to a file

//The banner element
s += '<h3 id="Banner_id" onclick="tests()">Group Membership</h3>\n';

//The status element, not to be confused with the status bar
s += '<p id="Status_id">Building Document...</p>\n';

//The user data element (holds PC name, user name, domain)
s += '<p id="UserData_id">No user data</p>\n';

//The global groups element
s += '<p id="GlobalGroups_id">No global groups<p>\n';

//The local groups element
s += '<p id="LocalGroups_id">No local groups<p>\n';

//Show the HTML
ie.document.body.innerHTML = s;

}
//End function BuildOutputWindow(){

//Shows HTML text in IE window in element labeled "Status_id"
function ShowStatus(sCurrStatus){
ie.document.all.item("Status_id").innerHTML = sCurrStatus;
}
//End function ShowStatus("sCurrStatus")
//Shows HTML text in IE window in element labeled "UserData_id"
function ShowUserData(){
var s = ""

//Puts it in a table
s += '<b>User Info</b> ';
s += '<table>\n';
s += '<tr><td width="33%" align=center><u>User Name</u></td>';
s += '<td width="33%" align=center><u>Domain Name</u></td>';
s += '<td align=center><u>Computer Name</u></td></tr>\n';
s += '<tr><td align=center>' + gsUserName + '</td>';
s += '<td align=center>' + gsUserDomain + '</td>';
s += '<td align=center>' + gsComputerName + '</td></tr>';
s += '</table>'
ie.document.all.item("UserData_id").innerHTML = s;
}
//End function ShowUserData()

//Shows HTML text in IE window in element labeled "GlobalGroupData_id"
function ShowGlobalGroupData(aGroups){
var s = ""; i = 0;

//Puts it in a table
s += '<b>Global Group Membership</b> ';
s += '<table cellspacing=10>\n'
s += '<tr><td width="25%"><u>Group Name</u></td>';
s += '<td><u>Description</u></td></tr>\n'
//Create each table row
for (i=0; i<aGroups.length; i++){
s += '<tr><td>' + aGroups[i].Name + '</td>';
s += '<td>' + aGroups[i].Description + '</td></tr>\n'
}
s += '</table>'
ie.document.all.item("GlobalGroups_id").innerHTML = s;
}
//End function ShowGlobalGroupData(){

//Shows HTML text in IE window in element labeled "LocalGroupData_id"
function ShowLocalGroupData(aGroups){
var s = ""; i = 0;

//Puts it in a table
s += '<b>Local Group Membership</b><br>';
s += '<table cellspacing=10>\n'
s += '<tr><td width="25%" valign=top><u>Group Name</u></td>';
s += '<td width="25%" valign=top><u>Gets Membership From</u></td>\n'
s += '<td valign=top><u>Description</u></td></tr>\n'
//Create each table row
for (i=0; i<aGroups.length; i++){
s += '<tr><td valign=top>' + aGroups[i].Name + '</td>';
s += '<td valign=top>' + gaMembershipFrom[i] + '</td>\n'
s += '<td valign=top>' + aGroups[i].Description + '</td></tr>\n'
}
s += '</table>'
ie.document.all.item("LocalGroups_id").innerHTML = s;
}
//End function ShowLocalGroupData(){
[/code]



If you run (save it as script.js or something else) the script, u get the desired information, the question ist how sould we place this script into a php page, to get these 2 values, when the student registers ??


any help would be appreciated

Ioannis E. Ntentopoulos
Omiros College, Greece
Link to comment
Share on other sites

First I would like to say that ActiveXObject will only work in IE.

Ok I will give you a nice createRequestObject function, written by Ober.

[code]
function createRequestObject() {
    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera...
        var xmlhttp = new XMLHttpRequest();
        if (xmlhttp.overrideMimeType)
xmlhttp.overrideMimeType('text/xml');
    }
else if (window.ActiveXObject) { // IE
        try {
            var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!xmlhttp) {
        alert('Giving up :( Cannot create an XMLHTTP instance');
        return false;
    }
return xmlhttp;
}

var http = createRequestObject();
[/code]

Now say you are wanting to post something to a php script.. You could do something like this.

[code]
function sendRequest() {
    var field = document.getElementById('yourfield').value;
    http.open('POST', 'yourscript.php');
    http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    //send the field to the file
    http.send('field=' + field);
    //the handle request is a function that will handle the response from the php file
    http.onreadystatechange = handleRequest;
}

function handleRequest() {
    if (http.readyState == 4) {
      //the response from the php file
      var response = http.responseText;
      if (response == 'your error message') {
          alert('There was an error in sending the request');
      } else {
          alert('It worked');
      }
    }
}[/code]

Good Luck,
Tom
Link to comment
Share on other sites

Tom, thank you for ur reply, hope im can get that thing to work , not so familiar in JS. Also, i know that ActiveXObject is only for IE, not very happy with that, but 4 now i have to accept that :(

I hope, that i can bother again, if i have problems

thank u in advance
Link to comment
Share on other sites

ok, here it is how far i could get through by myself

[code]
var  gsUserName, gsComputerName;

function Get_ComputerName(){
  var  WshNetwork  = new ActiveXObject ("WScript.Network");
  return (WshNetwork.ComputerName);
}

function Get_UserName(){
  var  WshNetwork  = new ActiveXObject ("WScript.Network");
  return  (WshNetwork.UserName);
}
[/code]

so... in my opinion the 2 GET functions (Get_ComputerName and Get_UserName) should show the required data if they where called somewhere in the file, right ???

my question is, how can i put this code into a single php file, the 2 GET functions should somehow do what they sould do, and afterwards, how to put the results of the 2 GETs into a simple php variable, something like

[code]
<?php
$variable_UserName = gsUserName ;
$variable_ComputerName = gsComputerName ;
?>
[/code]
Link to comment
Share on other sites

...as i said, i know nothng about JS :(

i did a lite version of my first code

[code]
var  ie = new ActiveXObject ("InternetExplorer.Application");

var  gsUserName, gsComputerName;

BuildOutputWindow();
  gsUserName    = Get_UserName();
  gsComputerName = Get_ComputerName();
  ShowUserData();

function Get_ComputerName(){
  var  WshNetwork  = new ActiveXObject ("WScript.Network");
  return (WshNetwork.ComputerName);
}

function Get_UserName(){
  var  WshNetwork  = new ActiveXObject ("WScript.Network");
  return  (WshNetwork.UserName);
}

function BuildOutputWindow(){
  var  n = 0;
  var  s = "";

  ie.height      = 480;
  ie.width      = 640;
  ie.menubar    = false;
  ie.toolbar    = false;
  ie.statusbar  = false;
  ie.addressbar  = false;
  ie.resizable  = true;
  ie.navigate ("about:blank");

  while (ie.busy) {++n};

  ie.document.body.innerHTML = "Building Document..." + "<br>load time= " + n;
  ie.visible = true;
  s += '<p id="UserData_id">No user data</p>\n';
  ie.document.body.innerHTML = s;

}

function ShowStatus(sCurrStatus){
  ie.document.all.item("Status_id").innerHTML = sCurrStatus;
}

function ShowUserData(){
  var  s = ""
  s += '<table>\n';
  s += '<tr><td width="33%" align=center><u>User Name</u></td>';
  s += '<td align=center><u>Computer Name</u></td></tr>\n';
  s += '<tr><td align=center>' + gsUserName + '</td>';
  s += '<td align=center>' + gsComputerName + '</td></tr>';
  s += '</table>'
  ie.document.all.item("UserData_id").innerHTML = s;
}
[/code]

saved it, and double clicked it, a popup window came up and the requested info was there.

there must be a way to merge that script into a single php file , or not ?

i posted also a screenshot with the info i got from the js, when i doubleclicked it

[attachment deleted by admin]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.