Posted: 23rd Nov 2021 15:21
Hi

Here is a simple example to set and Get an online hightscore.
- send the php script on your webserver, and use the AppGameKit code in your game .

AGK code :
Change "YourUrl.com" by your server url (line : MyURL$ = "YourURL.com").
+ Code Snippet
// Project: HighScore
// Created: 21-11-23

SetErrorMode(2)

// set window properties
SetWindowTitle( "HighScore (agk+php)" )
SetWindowSize( 1024, 600, 0 )
// set display properties
SetVirtualResolution( 1024, 600 ) // doesn't have to match the window
UseNewDefaultFonts( 1 )

// create variables
Global iHttp, delayHttpResponse, MyURL$

// the delay to get info from our "tchat.php" page
delayHttpResponse = 2000
// your url here : 
MyURL$ = "YourURL.com"


CreateText(1,"")
SetTextSize(1,20)

// send the highscore
pseudo$ = "Player"+str(random(0, 500))
Myscore = random(10, 5000)
Score$ = str(Myscore)
SendScore(Pseudo$, score$)


// main loop
do
	
	If GetPointerPressed()
		y = getpointery()
		StartTchaty = y -TchatY
	endif
	if GetPointerState()
		y = getpointery()
		TchatY = y -StartTchaty
		SetTextPosition(1,0,TchatY)
	endif
		
	Refresh()
    // Print( "Server response: " + response$ )
    Sync()
loop


Function SendScore(Pseudo$, score$)
	
	// create an http connection : 
	iHTTP = CreateHTTPConnection()
	SetHTTPHost( iHTTP, MyURL$, 0 )
	
	// if the server takes a long time to respond this could make the app unresponsive
	
	// it's necessary to encode it (for some words)
	Pseudo$ = HTTPEncode(Pseudo$)
	// but, it seems the HTTPEncode() doesn't works for accents.
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%A9",-1)
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%A8",-1)
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%A2",-1)
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%B4",-1)
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%A0",-1)
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%AA",-1)
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%B9",-1)
	Pseudo$ = ReplaceString(Pseudo$,"?","%C3%A7",-1)
	// the create the string for szServerFile for SendHTTPRequest()
	post$ = "highscore.php?Action=Post&Pseudo="+Pseudo$+"&Score="+Score$ 
	
	// we could use Asynchronous, that doesn't wait for the response, because we doesn't need it
	Response$= SendHTTPRequest( iHTTP, post$)
	// just if we want to check the status of the request()
	int= GetHTTPStatusCode( iHTTP )
	//~ message(REsponse$)
	//~ message(str(int))
	CloseHTTPConnection(iHTTP)
	DeleteHTTPConnection(iHTTP)
	
Endfunction

Function Refresh()
	
	// to refresh the connection to get the new message from the php script
	
	// you can change the delay if you want.
	IF delayHttpResponse > Screenfps()/2
		// create a new httpconnection
		iHTTP = CreateHTTPConnection()
		SetHTTPHost( iHTTP, MyURL$, 0 )
		// set the string for SendHTTPRequest
		url$ ="highscore.php?Action=Refresh"
		r$ = SendHTTPRequest( iHTTP, url$ )
		// then get the response, and if it's not "", we can add it to the highscore
		if r$ <> ""
			response$ = ReplaceString(r$,"<br/>",Chr(10),-1)
			response$ = ReplaceString(response$,"\'","'",-1)
			response$ = ReplaceString(response$,"\"+chr(34),chr(34),-1) // \"
			SetTextString(1,response$)
		endif
		// int= GetHTTPStatusCode( iHTTP )
		delayHttpResponse = 0
		CloseHTTPConnection(iHTTP)
		DeleteHTTPConnection(iHTTP)
    else
		inc delayHttpResponse
	endif
		
Endfunction 





Php script : to send to your webserver :
+ Code Snippet
<?php
if(!is_file('HighScore.txt'))
{
	$Score=array();
	file_put_contents('HighScore.txt', serialize($Score));
}

if(isset($_GET['Action']))
{
	if($_GET['Action']=='Post' AND isset($_GET['Pseudo']) AND isset($_GET['Score']))
	{
		$Score=unserialize(file_get_contents('HighScore.txt'));
		$Score[$_GET['Score']]=$_GET['Pseudo'].' : '.$_GET['Score'];
		file_put_contents('HighScore.txt', serialize($Score));		
	}
	elseif($_GET['Action']=='Refresh')
	{
		$Score=unserialize(file_get_contents('HighScore.txt'));
		if ($Score!="")
		{
			krsort($Score);
			foreach($Score as $key => $val)
			{
				echo $val.'<br/>';
			}
		}
	}
}
?>


Cheers !
Posted: 24th Nov 2021 6:10
To make purple token work with itch.io I had to add the following to the PHP:

header("Access-Control-Allow-Origin: *");

I would also recommend using SSL/TLS and some sort of private identifier sent with the data, otherwise anyone could access the file.