Page 1 of 2
Problem with WriteCGIString
Posted: Tue Sep 21, 2021 3:23 am
by loulou2522
Code: Select all
UseMySQLDatabase()
If Not InitCGI() Or Not ReadCGI()
End
EndIf
user$=CGIParameterValue("username")
sql=OpenDatabase(#PB_Any,"host=xxx port=3306 dbname=xxxx","xxx",xx",#PB_Database_MySQL)
If sql
If DatabaseQuery(sql,"select user from login_user where nom='"+user$+"';")
If NextDatabaseRow(sql)
resultat$=GetDatabaseString(sql,0)
Else
resultat$=""
EndIf
EndIf
CloseDatabase(sql)
Else
resultat$=DatabaseError()
EndIf
WriteCGIHeader("Access-Control-Allow-Origin", "*") ;<----------- Remove for Final Release !!!!!!
WriteCGIHeader(#PB_CGI_HeaderContentType,"text/html",#PB_CGI_LastHeader)
WriteCGIString(user$+"="+resultat$)
I have a problem because the instruct
WriteCGIString(user$+"="+resultat$)
return the value of user$ but an empty value for resultat$
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 5:44 am
by Paul
WriteCGIString is working fine since it returns a value for user$
If resultat$ is empty it is because your Query is not returning what you expect.
Maybe your table name is wrong? Maybe your column name is wrong? We don't know the layout of your database so we can only guess.
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 7:00 am
by loulou2522
I execute the same query with PHPADMIN and the query return the value of user. I debug the result just before writnh WriteCGISring and the value was the same Maybe a bug ?
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 7:14 am
by Kiffi
Maybe 'username' contains special characters (spaces, umlauts, accents, quotes, etc.)?
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 8:23 am
by loulou2522
No username doesn't content any special character. It seems that the variable which contain the resutl of the query is not transmetted or transmetted blank in writeCGI , if i create a var in the programm , this var is well transmetted to writecgi
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 9:25 am
by Kiffi
Can you please post the HttpRequest() command?
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 10:30 am
by loulou2522
It's in SPIDERBASIC
When invoking
HTTPRequest(#PB_HTTP_Post, url, "username=LOUVET", @HttpGetEvent())
Code: Select all
Procedure HttpGetEvent(Success, Result$, UserData)
If Success
debug result$
If result$="true"
StickyWindow(#wndLogin,#true)
CloseWindow(#wndLogin)
DisableWindow(#wndMain, #false)
else
SetGadgetText(#txterrormessage,"Login et/ou Mot de passe invalide")
SetActiveGadget(#strLoginUsername)
endif
Else
Debug "Error: No Connection"
EndIf
EndProcedure
If you want i can post the whole programm in spiderbasic and the part in purebasic
Note tha if i invoke a php file instead of the exe file the CGI works well
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 12:09 pm
by infratec
Mabe you need to escape the = with %3D
Code: Select all
WriteCGIString(user$+"%3D"+resultat$)
Check the real data with WireShark or tcpdump.
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 2:52 pm
by Paul
What do you get is you call the cgi from your web browser?
https://yourdomain.com/cgi-bin/cgi.exe?username=LOUVET
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 4:55 pm
by loulou2522
I think i have found the problem
When i create a pure basic like that
Code: Select all
sql = OpenDatabase(#PB_Any,"host=xxx port=3306 dbname=xxx","xxx","xxx",#PB_Database_MySQL)
If sql
If DatabaseQuery(sql,"select user,nom from login_user where nom="+Chr(39)+ user$+Chr(39)+";",#PB_Database_DynamicCursor)
If NextDatabaseRow(sql)
resultat$=Trim(GetDatabaseString(sql,0))
Else
resultat$=""
EndIf
Else
EndIf
FinishDatabaseQuery(sql)
CloseDatabase(sql)
Else
resultat$=DatabaseError()
EndIf
The result is good and resultat$ contain the value of user
If i use cgi like that
Code: Select all
if Not InitCGI() Or Not ReadCGI()
End
EndIf
sql = OpenDatabase(#PB_Any,"host=xxx port=3306 dbname=xxx",x"xx","xxx",#PB_Database_MySQL)
If sql
If DatabaseQuery(sql,"select user,nom from login_user where nom="+Chr(39)+ user$+Chr(39)+";",#PB_Database_DynamicCursor)
If NextDatabaseRow(sql)
resultat$=Trim(GetDatabaseString(sql,0))
Else
resultat$=""
EndIf
Else
EndIf
FinishDatabaseQuery(sql)
CloseDatabase(sql)
Else
resultat$=DatabaseError()
EndIf
WriteCGIHeader("Access-Control-Allow-Origin", "*") ;<----------- Remove for Final Release !!!!!!;
WriteCGIHeader(#PB_CGI_HeaderContentType,"text/html",#PB_CGI_LastHeader)
WriteCGIString(user$+"%3D"+resultat$)
The progamm is unable to open database the result of sql is 0 , databaseeror is empty , that's while resultat$ is empty
Did you think it's a bug ?
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 5:36 pm
by Paul
If your "host" parameter "localhost" and your cgi is compiled as a console app and it is unable to connect to your database, maybe its a permissions issue with your server setup since you say it connects fine running outside your server?
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 5:45 pm
by Kiffi
Please try the following:
Code: Select all
EnableExplicit
If Not InitCGI() Or Not ReadCGI()
End
EndIf
Define user$
user$ = CGIParameterValue("username")
WriteCGIHeader("Access-Control-Allow-Origin", "*")
WriteCGIHeader(#PB_CGI_HeaderContentType,"text/html",#PB_CGI_LastHeader)
WriteCGIString(user$)
This way we can make sure that you call your CGI correctly.
Your HTTPRequest() then returns LOUVET in HttpGetEvent() as Result$.
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 6:10 pm
by loulou2522
I confirm two things :
- the programm works with your example
- there 's a bug when i open database
sql = OpenDatabase(#PB_Any,"host=localhost port=3306 dbname=xxx","xxx","xxx",#PB_Database_MySQL)
sql return 0 indcating an error
If you have a mysql database you can reproduce the bug connecting to this database
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 6:29 pm
by Paul
If you have a mysql database you can reproduce the bug connecting to this database
This seems to be a problem unique to your setup since I have many cgi apps written in PureBasic which connect to both MySQL databases and Postges databases without any issues.
Re: Problem with WriteCGIString
Posted: Tue Sep 21, 2021 7:11 pm
by loulou2522
When i use a php file the result is good with cgi.
I use IIS10 express , can-you help me to verify the configuration ?
Thanks