Problem with WriteCGIString

Just starting out? Need help? Post your questions and find answers here.
loulou2522
Enthusiast
Enthusiast
Posts: 495
Joined: Tue Oct 14, 2014 12:09 pm

Problem with WriteCGIString

Post 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$
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Problem with WriteCGIString

Post 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.
Image Image
loulou2522
Enthusiast
Enthusiast
Posts: 495
Joined: Tue Oct 14, 2014 12:09 pm

Re: Problem with WriteCGIString

Post 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 ?
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Problem with WriteCGIString

Post by Kiffi »

Maybe 'username' contains special characters (spaces, umlauts, accents, quotes, etc.)?
Hygge
loulou2522
Enthusiast
Enthusiast
Posts: 495
Joined: Tue Oct 14, 2014 12:09 pm

Re: Problem with WriteCGIString

Post 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
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Problem with WriteCGIString

Post by Kiffi »

Can you please post the HttpRequest() command?
Hygge
loulou2522
Enthusiast
Enthusiast
Posts: 495
Joined: Tue Oct 14, 2014 12:09 pm

Re: Problem with WriteCGIString

Post 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
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Problem with WriteCGIString

Post 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.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Problem with WriteCGIString

Post by Paul »

What do you get is you call the cgi from your web browser?

https://yourdomain.com/cgi-bin/cgi.exe?username=LOUVET
Image Image
loulou2522
Enthusiast
Enthusiast
Posts: 495
Joined: Tue Oct 14, 2014 12:09 pm

Re: Problem with WriteCGIString

Post 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 ?
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Problem with WriteCGIString

Post 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?
Image Image
User avatar
Kiffi
Addict
Addict
Posts: 1353
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Problem with WriteCGIString

Post 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$.
Hygge
loulou2522
Enthusiast
Enthusiast
Posts: 495
Joined: Tue Oct 14, 2014 12:09 pm

Re: Problem with WriteCGIString

Post 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
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Problem with WriteCGIString

Post 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.
Image Image
loulou2522
Enthusiast
Enthusiast
Posts: 495
Joined: Tue Oct 14, 2014 12:09 pm

Re: Problem with WriteCGIString

Post 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
Post Reply