Jump to content

PHP/VB


sanchez77

Recommended Posts

How do you pass a PHP variable to a VBS function.

 

For example, here the test.vbs file:

 


Function WriteLineToFile(ntext)
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForWriting, True)
f.WriteLine ntext
Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function

WriteLineToFile($arg1)

 

PHP file, test.php

 


$arg1 = 'test 55';
Exec('test.vbs $arg1');

 

I want to take the variable $arg1 and pass it to the vbs function.  Has anyone done something similar or could point me in the right direction?

 

Thanks,

sanchez

Link to comment
Share on other sites

You aren't running that section of code from VB, but from PHP, which will not parse variables inside of single quotes (unless those single quotes are inside double quotes).  Try changing them to double quotes and you will pass the value of that variable.

Link to comment
Share on other sites

google tells me you're not grabbing the arguments correctly in your VBS. It should be:

 

Function WriteLineToFile(ntext)
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForWriting, True)
f.WriteLine ntext
Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function

Dim Arg, var1
Set Arg = WScript.Arguments
var1 = Arg(0)
WriteLineToFile(var1)

Link to comment
Share on other sites

yay google!!!!

 

Thanks batwimp, i'm new to VB as well, appreciate the insight.

 

In the example below it's dropping the Text part of the variable. Any ideas on that?

 

<?php

$arg1 = 'Test Text';
exec("test.vbs $arg1");

?>

 

I tried both

 

$arg1 ='Test Text';

 

and

 

$arg1 = "Test Text";

 

But it still drops the Text part 'Test Text' when writing to the text file.

Link to comment
Share on other sites

You are essentially issuing this command through the command line by using exec() from PHP. This means that your arguments are separated by a space instead of a comma. So it is treating 'text' as a separate argument instead of being part of the same string. You can set up your VBS to take multiple arguments by adding them in, which should be fairly obvious:

 

NOT TESTED

 

Function WriteLineToFile(ntext,ntext2)
Const ForReading = 1, ForWriting = 2
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForWriting, True)
f.WriteLine ntext+ntext2
Set f = fso.OpenTextFile("C:\inetpub\test\test.txt", ForReading)
WriteLineToFile = f.ReadAll
End Function


Dim Arg, var1, var1
Set Arg = WScript.Arguments
var1 = Arg(0)
var2 = Arg(1)
WriteLineToFile(var1, var2)

 

But if you want to pass the entire string as a single argument with the space(s) in them, I'm not sure how to do that.

Link to comment
Share on other sites

I found it. In order to pass the variable as a single argument with spaces, you need to enclose it in quotes, which means you need to quote the quotes:

 

$arg1 = '"test 55"';

 

Note the double quotes are inside the single quotes.

Link to comment
Share on other sites

b/c the old database i need to connect/write to is on a Unix Box.  The app I'm building is PHP on Windows Box. I need PHP SSH2 functions to connect to it with PHP but b/c windows sucks, I  need to be complie it into it's own version to support the SSH functions. I ran into trouble compling it on Win Srv 2008 box and this was the other choice i thought of to use in writing to the the Unix Box. I'm open to suggestion if you have a better idea.

 

PHP Links:

 

http://us3.php.net/manual/en/ssh2.requirements.php

http://us3.php.net/manual/en/ssh2.installation.php

 

Building PHP/Windows

https://wiki.php.net/internals/windows/stepbystepbuild

 

Other Post regarding compiling on x64

http://www.phpfreaks.com/forums/index.php?topic=358758.0

 

I'm able to compile on x86, but because I need to use SSH2 and OpenSSL, I need to complie on the x64 box.

 

I know, just use Unix\Linux. If I could I would.

 

Got a better idea?

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.