Web Hosting Talk







View Full Version : Passing a var from ASP to PHP (for a bot stopper)


Serevinus
01-07-2003, 04:31 AM
I am making a "bot stopper" for a webpage (a couple of words in an image that the user has to type to create an account), my problem is that the site is coded in ASP (I dont know a lot about PHP), I cannot create an image in ASP, I have to create the image in PHP and use the GD lib...

I want to put the words that will appear in the image, into a session (in the ASP page) and then use an image link to the PHP file, which will read the session and create the image... the user will then type the words into a form on the ASP page, which will be compared to the session variable... access will then be granted or denied

This is the code I am using...

botstop.asp
<%
Session("SessTest") = "Test Var"
Response.Write "<img src=""botstop.php"">"
%>

botstop.php
<?php
$img_number = imagecreate(300,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
imagefill($img_number,0,0,$white);
Imagestring($img_number,9,30,15,$_SESSION['SessTest'],$black);
header("Content-type: image/png");
imagepng($img_number);
?>

It currently only displays the image with nothing in it (just a white background), how can I get the image to show the words without needing to pass them in a querystring like <img src="botstop.php?words=Test+Var"> (which would kind of defeat the purpose of having a bot stopper in the first place)

i am a
01-07-2003, 04:57 AM
asp and php don't write to the same files when using sessions, so that won't work, but a possible solution is similar. you may want to write to a temporary file say, with asp, and then pick up the variable using php.

Serevinus
01-07-2003, 05:07 AM
Thats a possiblility
Or maybe I could add a record to a mysql database and pass the record id to the PHP page with a querystring?

Something like this?
<img src="botstop.php?id=<%=rsBotStop("id")%>">

sunpost
01-08-2003, 06:30 AM
i used another asp page, botstop.asp, to get the image using the session variable...pass the session variable in the querystring to the php, botstop.php.

i could not get the friggin relative URL to work in the xmlhttp...ms xml4 sdk doc sux...you may need to change the URL in the botstop.asp...if you can get it to use the relative URL let me know how you did it.

botstoptest.asp should make an image of the servers datetime.

botstop.asp
<%
Set oHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP.4.0")
oHTTP.Open "GET", "http://localhost/botstop.php?SessTest=" & Session("SessTest"), False
oHTTP.Send
Response.BinaryWrite oHTTP.ResponseBody
%>

botstop.php
<?php
$img_number = imagecreate(300,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);
imagefill($img_number,0,0,$white);
Imagestring($img_number,9,30,15,$_GET['SessTest'],$black);
header("Content-type: image/png");
imagepng($img_number);
?>

botstoptest.asp
<html>
<head>
<title>botstop test</title>
</head>
<body>
<%
Session("SessTest") = Now()
Response.Write "<img src=""botstop.asp"">"
%>
</body>
</html>

good luck.

Serevinus
01-09-2003, 05:05 AM
That looks good sunpost, but I am using a linux server with Sun One ASP (Chilisoft!ASP), so as far as I am aware, I cannot use ms xmlhttp
Do you know of anything similar that I can use? I have root so I can install things if needed. Or maybe there is a free Chili!Bean I can use or something?

I did find something called AmoebaHTTP (http://www.amoebasoft.com/ahttp.asp) but I'm not really willing to pay $50 for it just for a bot stopper (I wouldnt mind paying $25 for AmoebaRegExp (http://www.amoebasoft.com/regexp.asp) though)

sunpost
01-09-2003, 12:10 PM
it is lo-tech, but might work to stopbots...

<html>
<head>
<title>bs</title>
</head>
<body>
<%
Function bs(pstrText)
Dim i
Response.Write "<table border=""0"" cellspacing=""0"" cellpadding=""0"">" & vbNewLine & "<tr>" & vbNewLine
For i = 1 to Len(pstrText)
Response.Write "<td>" & Mid(pstrText,i,1) & "</td>" & vbNewLine
Next
Response.Write "</tr>" & vbNewLine & "</table>" & vbNewLine
End Function

Session("SessTest") = "foo"
Call bs(Session("SessTest"))
%>
</body>
</html>

Serevinus
01-09-2003, 01:06 PM
It wouldnt take a robot subscriber much to break up the table to get the word
I would rather use GD to make the image... so if you know another way to gethttp I would love to hear it
I did know another way, I used it an age ago but I have long since forgotten it (it was probably another Windows COM that wont work on nix anyway)

gaouzief
01-09-2003, 01:13 PM
hello

use a language independant two way encryption algorithm using the same key in your asp and php script
this way you will be abble to pass the crypted string to php like
<img src="script.php?hash=encrypted-string"> php will ecrypt it using the same k and display the original string

Regards,

Hassan El Forkani
http://www.WarmAfrica.com

sunpost
01-09-2003, 07:44 PM
:idea: you can use the fso to write the php code...you will need to clean up the files for expired sessions, but wtf--here it goes...

good luck!!!

<html>
<head>
<title>wtf</title>
</head>
<body>
<%
Function MakeIt(pstrText)
Dim MyFSO
Dim MyTextFile
Dim txtCode
Dim txtFileName
Dim txtFilePath

txtCode = "<?php" & vbNewLine
txtCode = txtCode & "$img_number = imagecreate(300,50);" & vbNewLine
txtCode = txtCode & "$white = imagecolorallocate($img_number,255,255,255);" & vbNewLine
txtCode = txtCode & "$black = imagecolorallocate($img_number,0,0,0);" & vbNewLine
txtCode = txtCode & "imagefill($img_number,0,0,$white);" & vbNewLine
txtCode = txtCode & "Imagestring($img_number,9,30,15,'" & pstrText & "',$black);" & vbNewLine
txtCode = txtCode & "header(""Content-type: image/png"");" & vbNewLine
txtCode = txtCode & "imagepng($img_number);" & vbNewLine
txtCode = txtCode & "?>" & vbNewLine

txtFileName = "makeunique.php"
txtFilePath = server.mappath("\") & "\" & txtFileName

Set MyFSO = Server.CreateObject("Scripting.FileSystemObject")
Set MyTextFile = MyFSO.CreateTextFile(txtFilePath , True)
MyTextFile.WriteLine(txtCode)
MyTextFile.Close
Set MyTextFile = Nothing
Set MyFSO = Nothing

MakeIt = txtFileName

End Function

Session("SessTest") = "foo"
MakeIt(Session("SessTest"))
Response.Write "<img src=""" & MakeIt(Session("SessTest")) & """>"
%>
</body>
</html>

Serevinus
01-09-2003, 08:40 PM
OK, I have a test page up with an example of what I have done :)
Seems to work fine, I added some lines in to prevent the text being read by bot that can read images... I made something that did that once so it is posible... however if the lines are too annoying and make the text too hard to read I will remove them...
The example is here... http://game.lotrforums.com/test/join.asp

Here is the code I used...
join.asp
<html>
<head>
<title>Join (Bot stopper test)</title>
</head>
<body bgcolor="white">

<%If Request.Form("action") <> 1 Then%>
<img src="botstop.asp" width="300" height="50" alt="Bot stopper"><br />
<form method="POST" action="join.asp">
<input type="text" name="botstopper" value="" size="20" maxlength="30"> it is case sensitive<br />
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="action" value="1">
</form>
<%
Else
If Request.Form("botstopper") = Session("BotStopperWord") Then
Response.Write "<font color=""green"">Looks like you typed the right botstopper words... you can now continue signing up</font>"
Else
Response.Write "<font color=""red"">Oh dear, you typed the wrong botstopper words, try again</font>"
End If
End If%>

</body>
</html>

botstop.asp
<%
Dim WordConn
Session("BotStopperWord") = GenerateBotStopperWord

Set AmoebaClient = Server.CreateObject("Amoeba.HTTP")
AmoebaClient.SetKey "MY-KEY"
AmoebaClient.Url="http://game.lotrforums.com/test/botstop.php?BotStopperWord=" & Session("BotStopperWord")
BinaryResult = AmoebaClient.FetchURL()
If AmoebaClient.GetErrorCode() = 0 Then
Response.ContentType = "image/png"
Response.BinaryWrite AmoebaClient.FetchBinaryURL
End If
%>

<%
Function GenerateBotStopperWord
strWordConn = "driver=MySQL;server=localhost;uid=serevinus;pwd=shaitan;database=lotrgame;"
Set WordConn = Server.CreateObject("ADODB.Connection")
WordConn.Open strWordConn

Randomize
rndNumber = Int((Rnd*999) + 1)
rndWord1 = GetWord("Words1")
rndWord2 = GetWord("Words2")
rndWord3 = GetWord("Words3")
GenerateBotStopperWord = rndNumber & rndWord1 & rndWord2 & rndWord3

WordConn.close
Set WordConn = nothing
End Function
%>

<%
Function GetWord(TableName)
sqlQuery = "SELECT Word FROM " & TableName & " WHERE Id = " & Int((Rnd*25) + 1)
Set rsWordConn = WordConn.Execute (sqlQuery)
GetWord = rsWordConn("Word")
rsWordConn.Close
Set rsWordConn = nothing
End Function
%>

botstop.php
<?php
$img_number = imagecreate(300,50);
$white = imagecolorallocate($img_number,255,255,255);
$black = imagecolorallocate($img_number,0,0,0);

imagefill($img_number,0,0,$white);
Imagestring($img_number,9,30,18,$_GET['BotStopperWord'],$black);

$x1 = get_random(295);
$x2 = get_random(295);
$y1 = get_random(45);
$y2 = get_random(45);
imageline($img_number, $x1, $y1, $x2, $y2, $black);

$x1 = get_random(295);
$x2 = get_random(295);
$y1 = get_random(45);
$y2 = get_random(45);
imageline($img_number, $x1, $y1, $x2, $y2, $black);

header("Content-type: image/png");
imagepng($img_number);

function make_seed() {
list($usec, $sec) = explode(' ', microtime());
return (float) $sec + ((float) $usec * 100000);
}

function get_random($max)
{
srand(make_seed());
return rand(1,$max);
}
?>

The database is simply 3 tables with different sets of words in (words1 are descriptive words, words2 are colours, words3 are objects)
Some of the combinations it comes up with can be quite funny

Let me know what you think :)

sunpost
01-09-2003, 08:53 PM
Server.CreateObject("Amoeba.HTTP")

cheater :stickout:

Serevinus
01-09-2003, 09:12 PM
hehe, I havnt bought it (yet)... its only a 30 day demo
Tried using a Java Bean thingy but I couldnt figure it out...

I'm not sure whether or not I should buy it... its not really worth 50 dolars just to fetch a wepage from a site is it... I may well get AmoebaCommand though, will be very handy for making my own User CP, combine it will Chili!Upload and I can have a nice alternative to FTP... could also be used to start and stop an IRC server if a customer orders one

sunpost
01-09-2003, 10:07 PM
AmoebaCommand looks very useful.

it seems like the sun asp is very limited compared to ms version...i would suggest spending the 50$ on a php book...i think you will find many more resources for the linux os using php.

good luck:beer:

Serevinus
01-10-2003, 06:02 AM
Yeah, I have been meaning to learn a little PHP for a while now, but never found the time to learn more than a little
Any books you would recommend to a relative PHP newbie? come to think of it, I have never seen a book which has anything about GD, which is what I tend to use PHP for at the moment, you can probably see my GD skills are prety limited

sunpost
01-10-2003, 06:16 AM
actually, i only use asp/asp.net...i am sure searching this forum will produce some titles for ya...if i get a book, i usually stick to the same publishers--o'reilly, wrox & ms press--i got used to the writing style of these companies.

Serevinus
01-10-2003, 06:35 AM
ok, I'll look arround, I also use asp for just about everything, why did sun have to leave regexps out of asp? I have to break into jsp to use them, and I dont know a lot about jsp (this coming from a guy with level 3 c++)
I tend to stick to O'Reilly books, though I may buy a Sams book if it looks good enough (their book on SQL is quite handy, i'm always refering to it)
Do you know any good Linux Bibles? I have been meaning to get one, but nobody seems able to recommend one... Adminning a RaQ4 with virtualy no terminal skills isnt easy... slowly getting the hang of it though :)

Thanks for all the help, some of its been very useful ;)

sunpost
01-10-2003, 06:58 AM
sun may have focused their development on objects that did not exist in vbscript or jscript...so the regex in vbscript may have taken the back seat to the all important Ad Rotator Object:crap:

cant help you with the linux stuff...gave it up years ago for the wizards of ms:dgrin:

cheers

ServerCorps
01-12-2003, 04:27 PM
dude, try this:
Client side jscript block works well. I call asp pages all the time.Just remember that the phpcode must output jscript code, like


<script src="somefile.php?allpertinentinforhere=123"></script>
in somefile.php:
document.write <? phpcode here ?>




That's how I share info from various languages, scripts, client code, etc.