0

I'm building a game application that uses redis and this is my php script.

<?php
//melon game pop

//connect to redis db
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);


//receive the data as json
//$data = json_decode(file_get_contents('php://input'), true);
$data = null;


//decided to just grab the string
$key = file_get_contents('php://input');

//Shotgun debugging in action
$my_key = trim($key);

/*
 was originally sending json on reading into variables from array decided to simplify until my issue is resolved
//parse my data in to separate variables  
if ($data != null) {
$key = $data['key'];
//$value = $data['value'];  

//$key = utf8_decode($key);        
}
else
{
print "It's not recognizing your data!";
}
*/




$my_echo = $redis->rpop($my_key);

 //it returns false if the key doesn't exist, but i've shown that it clearly exists so  why is it returning false???

if ($my_echo == false) {

print $my_key;
//print '5'.$key;

//print "5failure";

}
else
{
print $my_echo;
}
?>

What I have going on in the above code is a game client sends $key to php and I want to pop the value stored under $key which in this case was "ubuntu5". In my debug block when I print $my_key it responds with "ubuntu5", so I'm struggling to figure out why it won't print the value stored under $my_key. Furthermore, when I open redis-cli from the terminal and run rpop "ubuntu5" it responds with the expected value.

Any ideas on what could be going on here? { "key": "\nubuntuleft22" } I used var_dump, and also added this to the script:

$redis->lpush("my_control",$my_key);

When I pop "my_control" I get { "key": "\nubuntuleft22" } from one client and just ubunturight22 from the other using a browser. When I manually pop from the command line I get the below:

redis-cli
127.0.0.1:6379> rpop my_control
"ubunturight22"
127.0.0.1:6379> rpop my_control
"{ \"key\": \"\\nubuntuleft23\" }"
127.0.0.1:6379> rpop "my_control"
"ubunturight23"
127.0.0.1:6379
Pfrex
  • 159
  • 2
  • 13
  • Try to var_dump($my_echo) and check the if the key was correct? – vijaykumar May 23 '18 at 06:02
  • I used var_dump on $my_echo and $my_key and the result:bool(false) string(13) "ubunturight20" which was exactly what I was expecting. Besides the double quotes, but that must be a function of var_dump() I'm guessing? – Pfrex May 23 '18 at 06:22
  • Just try var_dump($redis->keys("*")); before rpop and if the key was exits or not – vijaykumar May 23 '18 at 06:31
  • Yes, it would appear that my key is not visible when doing as you instructed. – Pfrex May 23 '18 at 06:50

0 Answers0