I have a script that loops through 3 files.
Each file posts data to a api and returns a result, either Accepted or Rejected in variable $result.
On a Accepted response the script stops running and echo out the result.
On a Rejected it echo's out the rejected response and carries on to the next file.
The problem is : Lets say all 3 files give a Rejected reponse , it echo's out 3 rejected responses.
How can i only echo out a single generic response if it does not get a Accepted Response.
foreach($getSeq as $key){
$fileName = $key->file;
include_once 'Lenders/' . $fileName;
if($result == 'Accepted'){
echo 'Accepted';
break;
}
if($result == 'Rejected'){
echo 'Rejected';
}
}
you can do it with array() like below:
<?php
$resultArr = array(); // suppose we insert below Accepted = 1 and Rejected = 0
foreach($getSeq as $key){
$fileName = $key->file;
include_once 'Lenders/' . $fileName;
if($result == 'Accepted'){
echo "Accepted";
$resultArr[] = 1;
break;
}
if($result == 'Rejected'){
$resultArr[] = 0;
}
}
if(!in_array(1, $resultArr)){
echo 'Rejected';
}
Try this, this is specifically for 3 reject response
$failed = 0;
foreach($getSeq as $key){
$fileName = $key->file;
include_once 'Lenders/' . $fileName;
if($result == 'Accepted'){
echo 'Accepted';
break;
}
if($result == 'Rejected'){
$failed++;
}
}
if($failed == 3) {
echo "Rejected";
}
You didn't said if what will happens to other ratio (1A:2R, 2A:1R), so I just created it for 3 rejects
Related
I am trying to show an echo when a user improperly enters their license key, though, for some reason, the message appears twice.
$invalidkey = '<!DOCTYPE HTML>
<html>
<body>
<center>
<div class="container2"
<h1>Your Product Key is Invalid!</h1>
</div>
</center>
</body>
</html>
';
if ($resulto->num_rows > 0)
{
while($row = $resulto->fetch_assoc())
{
$user_group = $row["LicenseKey"];
$days = $row["Count"];
// Key is valid
if ($user_group == $key)
{
$keyvalidated = true;
echo $user_group;
echo $key;
}
// Key is invalid
else if ($usergroup !== $key)
{
echo $invalidkey;
$keyvalidated = false;
}
}
}
Here is an image of the error actually appearing:
My guess is that for whatever reason there are actually two records in the result set. The best fix would be to find a way to run the right query, which just returns a single record for a single user. As a quick fix, perhaps just check the first record:
if ($resulto->num_rows > 0) {
$row = $resulto->fetch_assoc();
$user_group = $row["LicenseKey"];
$days = $row["Count"];
// Key is valid
if ($user_group == $key) {
$keyvalidated = true;
echo $user_group;
echo $key;
}
// Key is invalid
else if ($usergroup !== $key) {
echo $invalidkey;
$keyvalidated = false;
}
}
Again, you need to find out why the result set has two, or more than one, records.
Please try adding a break since there could be more than one iteration (or you wouldn't need a loop):
// Key is invalid
else if ($usergroup !== $key)
{
echo $invalidkey;
$keyvalidated = false;
break;
}
The reason why it's showing 2 times is because query is fetching 2 results. So you need to limit query to just 1, just read the first record, or break the loop after 1st iteration.
while($row = $resulto->fetch_assoc())
{
$user_group = $row["LicenseKey"];
$days = $row["Count"];
// Key is valid
if ($user_group == $key)
{
$keyvalidated = true;
echo $user_group;
echo $key;
}
// Key is invalid
else if ($usergroup !== $key)
{
echo $invalidkey;
$keyvalidated = false;
}
break;
}
I found this very interesting project on github: https://github.com/brannondorsey/apibuilder
After starting using it, I noticed that the request limitation won't work properly. The value for the requests in the MySQL database counts to "1000" (or the limit set in $hits_per_day), but the API output is still some data from the database.
The expected error message is simply attached to the API response.
But I would like to have an API that doesn't allow any output and only the error message. Below you can find the concerning file.
public function get_json_from_assoc(&$get_array){
$json_obj = new StdClass();
$pretty_print = $this->pretty_print;
if(!$this->find_config_errors()){
if(isset($get_array['pretty_print'])){
if(strtolower($get_array['pretty_print']) == "true") $pretty_print = true;
if(strtolower($get_array['pretty_print']) == "false") $pretty_print = false;
}
//if API is public or if API is private and a correct private key was provided
if(!$this->private ||
$this->private &&
isset($get_array['private_key']) &&
$this->private_key == $get_array['private_key']){
$query = $this->form_query($get_array);
if($this->check_API_key()
|| !$this->API_key_required){
//if search was included as a parameter in the http request but it isn't allowed in the api's config...
if(isset($get_array['search']) &&
!$this->search_allowed){
$json_obj->error = "search parameter not enabled for this API";
}
else if(isset($get_array['exclude']) &&
!$this->exclude_allowed){
$json_obj->error = "exclude parameter not enabled for this API";
}else{
if($results_array = Database::get_all_results($query)){
if(is_array($results_array)){
// deletes key => value pairs if the value is empty. Only works if array is nested:
// http://stackoverflow.com/questions/5750407/php-array-removing-empty-values
$results_array = array_filter(array_map('array_filter', $results_array));
foreach($results_array as $result_array){
foreach($result_array as $key => $value){
if($key == "COUNT(*)"){
$count = $value;
break;
}
}
}
if(!isset($count)) $json_obj->data = $results_array;
else $json_obj->count = $count;
//COME BACK need to make count only parameter work
}
}else $json_obj->error = "no results found";
}
//only attempt to increment the api hit count if this method is called from a PUBLIC API request
if($this->API_key_required){
$query = "SELECT " . $this->API_hit_date_column_name . " FROM " . Database::$users_table . " WHERE " . $this->API_key_column_name . " = '" . $this->API_key . "' LIMIT 1";
$result = Database::get_all_results($query);
//increments the hit count and/or hit date OR sets the error message if the key has reached its hit limit for the day
if($this->update_API_hits($this->API_key, $result[0][$this->API_hit_date_column_name]) === false){
$json_obj->error = "API hit limit reached";
}
}
}else $json_obj->error = "API key is invalid or was not provided";
//if there was a search and it returned no results
if($this->search != "" &&
!$this->search_has_been_repeated &&
isset($json_obj->error) &&
strstr($json_obj->error, $this->no_results_message) == true){
$this->search_in_boolean_mode = true; //set search in boolean mode to true
$this->search_has_been_repeated = true; //note that the search will now have been repeated
//$this->JSON_string = $this->get_json_from_assoc($get_array, $object_parent_name); //recurse the function (thus re-searching)
return $this->get_json_from_assoc($get_array); //recurse the function (thus re-searching)
}
}else{ //API is private but private_key is invalid or was not provided
$json_obj->error = "this API is private and the private key was invalid or not provided";
}
}else{ //config errors were present
$pretty_print = true; //always output errors in pretty print for readability
$json_obj->config_error = $this->config_errors;
}
return ($pretty_print && version_compare(PHP_VERSION, '5.4.0') >= 0) ? json_encode($json_obj, JSON_PRETTY_PRINT) : json_encode($json_obj);
}
I am trying to GET different rows from different columns in php/mysql, and pack them into an array. I am able to successfully GET a jason encoded array back IF all values in the GET string match. However, if there is no match, the code echos 'no match', and without the array. I know this is because of the way my code is formatted. What I would like help figuring out, is how to format my code so that it just displays "null" in the array for the match it couldn't find.
Here is my code:
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode($fbaddra);
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['addr'];
} else {
$fbaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['addr'];
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
UPDATE: The GET Request
I would like the GET request below to return the full array, with whatever value that didn't match as 'null' inside the array.
domain.com/api/core/engine.php?a=fbaddra&facebook=username&facebookp=pagename
The GET above currently returns null.
Requests that work:
domain.com/api/core/engine.php?a=fbaddra&facebook=username or domain.com/api/core/engine.php?a=fbaddra&facebookp=pagename
These requests return the full array with the values that match, or null for the values that don't.
TL;DR
I need assistance figuring out how to format code to give back the full array with a value of 'null' for no match found in a row.
rather than assigning as 'null' assign null. Your full code as follows :
include '../db/dbcon.php';
$res = $mysqli->query($q1) or trigger_error($mysqli->error."[$q1]");
if ($res) {
if($res->num_rows === 0)
{
echo json_encode('no match');
}
else
{
while($row = $res->fetch_array(MYSQLI_BOTH)) {
if($_GET['a'] == "fbaddra") {
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = null;
}
if ($row['facebookp'] === $_GET['facebookp']) {
$fbpaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fbpaddr = null;
}
$fbaddra = (array('facebook' => $fbaddr, 'facebookp' => $fbpaddr));
echo json_encode($fbaddra);
}
}
}
$mysqli->close();
You can even leave else part altogether.
Check your code in this fragment you not use same names for variables:
if ($row['facebook'] === $_GET['facebook']) {
$fbaddr = $row['dogeaddr'];
//echo json_encode($row['dogeaddr']);
} else {
$fpaddr = 'null';
}
$fbaddr not is same as $fpaddr, this assign wrong result to if statement.
It was the mysql query that was the problem.
For those who come across this, and need something similar, you'll need to format your query like this:
** MYSQL QUERY **
if ($_GET['PUTVALUEHERE']) {
$g = $_GET['PUTVALUEHERE'];
$gq = $mysqli->real_escape_string($g);
$q1 = "SELECT * FROM `addrbook` WHERE `facebookp` = '".$gq."' OR `facebook` = '".$gq."'";
}
** PHP CODE **
if($_GET['PUTVALUEHERE']{
echo json_encode($row['addr']);
}
I've been trying to make a CLI application that logs Yahoo! messenger login dates/times for certain users using a third party, but this isn't really getting anywhere. Even though checking iself works when used individually, it does not seem to when using the while & foreach too. checkAvailability outputs "000". Could anyone please fix this and perhaps optimize it?
<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";
function checkAvailability($user){
$dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
$fullText = explode("|", $dataGot);
$status_coded = $fullText[0];
echo $status_coded;
return $status_coded;
}
while(true) {
foreach($users as $user) {
$user['oldstatus'] = $user['status'];
if (checkAvailability($user) == "1" and $user['oldstatus'] != "online") {
$user['status'] = "online";
echo "online";
} elseif (checkAvailability($user) == "3" and $user['oldstatus'] != "invisible") {
$user['status'] = "invisible";
echo "invisible";
} elseif (checkAvailability($user) == "2" and $user['oldstatus'] != "offline") {
$user['status'] = "offline";
echo "offline";
} else {
$user['status'] = "error";
echo "error";
}
if ($user['status'] != $user['oldstatus']) {
echo $user." a fost detectat ca ".$user['status']." la ".date(DATE_RFC822).".\n";
}
}
sleep(60);
sleep(60);
}
An endless runing CLI application in PHP in not the best solution. Its better when you make a cronjob and run the script every one or every to minutes. Then you can store the status or what you need in a database or a file.
I have looked at your scripted and test the script:
<?php
error_reporting(E_ALL);
$users[0] = "|59|62|157|85|218|78|135|43|63|145|151|173|157|93|107|90|84|129|140|110|55|28|210|212|80|128|252|127|15|192|223|154|177|39|129|191|62|17|113|236|2|168&t=0.23704720849047";
$users[1] = "|70|255|229|124|194|244|242|223|73|250|184|237|222|251|8|243|104|4|70|125|205|177|229|255|178|244|123|251|13|157|220|47|88|247|15|0&t=0.04614829820959876";
function checkAvailability($user){
$dataGot = file_get_contents("http://www.imvisible.ro/getstatus.php?id=".$user);
$fullText = explode("|", $dataGot);
$status_coded = $fullText[0];
return $status_coded;
}
while(true) {
foreach($users as $key => $user) {
$userStatus[$key] = checkAvailability($user);
if(!isset($userStatusRet[$key]['oldstatus'])) {
$userStatusRet[$key]['oldstatus'] = '';
}
if(!isset($userStatusRet[$key]['status'])) {
$userStatusRet[$key]['status'] = '';
}
$userStatusRet[$key]['oldstatus'] = $userStatusRet[$key]['status'];
if ($userStatus[$key] == "1" and $userStatusRet[$key]['oldstatus'] != "online") {
$userStatusRet['status'] = "online";
echo "User ".$key.": online\n";
} elseif ($userStatus[$key] == "3" and $userStatusRet[$key]['oldstatus'] != "invisible") {
$userStats['status'] = "invisible";
echo "User ".$key.": invisible\n";
} elseif ($userStatus[$key] == "2" and $userStatusRet[$key]['oldstatus'] != "offline") {
$userStatusRet[$key]['status'] = "offline";
echo "User ".$key.": offline\n";
} else {
$userStatusRet[$key]['status'] = "error";
echo "User ".$key.": error\n";
}
}
sleep(5);
}
Its not the best solution but the problem in your case is, that you can't write in the $user output. I have made here a new variable with the user id as iterator. When you run the script you can see the output:
User 0: online
User 1: offline
User 0: online
User 1: error
User 0: online
User 1: offline
I have coded a nice script but i am constantly getting
Error on line 29: Parse error, unexpected T_IF(if)
I have tried debugging code, wasted plenty of time. But nothing, came out.
Here is my code.
<?php
include("geoip.inc");
$ip=$_SERVER['REMOTE_ADDR'];
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, "$ip");
$referrer=$_SERVER['HTTP_REFERER'];
// Country name is not used so commented
// Get Country Name based on source IP
//$country = geoip_country_name_by_addr($gi, "$ip");
$real=0;
geoip_close($gi);
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
}
else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time()+315360000);
if ($country_code="IN") {
if(preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY='.$key.'&IP='.$ip);
$real=$result
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
{setcookie("testcookie", "testvalue");
if( isset( $_COOKIE['testcookie'] ) ) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs){
$real=1;
}
}
else
$real=1;
}
else
$real=1;
} else
$real = 1;
}
else {
$real = 1;
}
} }
if ($real==1) {
include_once('Biggenius1.htm');
}
?>
It is if inside. Please give me advice, on how can i avoid these error. And also is there any alternative to code such complex script with multiple nested if statements?
Please post entire code:
try this
$real = 0;
geoip_close($gi);
if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
$real = 1;
} else {
if ($_COOKIE['iwashere'] != "yes") {
setcookie("iwashere", "yes", time() + 315360000);
if ($country_code = "IN") {
if (preg_match('/google/i', $referrer)) {
$key = "g17x9erm28n7cgifddssfqhgorjf3e"; // Account API Key
$ip = $_SERVER['REMOTE_ADDR']; // IP to Lookup
$result = file_get_contents('http://www.ipqualityscore.com/api/ip_lookup.php?KEY=' . $key . '&IP=' . $ip);
$real = $result;
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's {
if ($real == 0) {
setcookie("testcookie", "testvalue");
if (isset($_COOKIE['testcookie'])) {
if (isset($_POST['jstest'])) {
$nojs = FALSE;
} else {
}
// create a hidden form and submit it with javascript
echo '<form name="jsform" id="jsform" method="post" style="display:none">';
echo '<input name="jstest" type="text" value="true" />';
echo '<script language="javascript">';
echo 'document.jsform.submit();';
echo '</script>';
echo '</form>';
// the variable below would be set only if the form wasn't submitted, hence JS is disabled
$nojs = TRUE;
}
if ($nojs) {
$real = 1;
}
}
else
$real = 1;
}
else
$real = 1;
} else
$real = 1;
}
else {
$real = 1;
}
}
if ($real == 1) {
include_once('Biggenius1.htm');
}
On line 29, $real=$result should end in a semi-colon and on the following line {if($real==0) should be if($real==0){.
The error message is your friend, it suggested you look to line 29.
You placed a curely braces before the if condition
//$result will be equal to 1 for detected proxies & vpns or equal to 0 for clean IP's
{if($real==0)
remove it then your error wil be removed
From reading over your code, it seems like the only errors I can find are these:
{if($real==0)
And:
$real=$result
Which should be changed into:
if($real==0){
And:
$real=$result;
Here are the few errors I found:
if ($country_code="IN") : This is an assignment not comparision, will always return true
$real=$result : Missing Termination ; on the end