how to send string response in php [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am trying to send the string response of a particular request from ftp Interface. I am using below code..
if($s == 'Successfull') {
$rid = 's'.$request_id;
}
if($s == 'Unsuccessfull') {
$rid = 'e'.$request_id;
}
echo "<b style='font-size: 16px;color:#59AB8F;'>Your Request ID:".$rid.".......<b>\n";
printf("<br>");
header($rid);

Why not set a response code with a JSON response?
if($s == 'Successfull'){
http_response_code(201);
$data = [ 'id' => $request_id ];
}
else {
http_response_code(400);
}
if (isset($data)) {
header('Content-Type: application/json');
echo json_encode($data);
}
That would be the normal way of handling an API POST response. 201 signifies a resource was created, 400 signifies a bad request (you can change this if a different response code matches better). Then the client can read in the JSON data and get the id.

Related

I am wondering how can i rate limit my php api so a ip or client cant use it for 5 minutes [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
So, i have a super basic php api for my website and i need it to be rate limited.
Like a cooldown so the ip cant access the api for 5 minutes.
Thanks in advance =)
There's a couple ways you could do it, in order of preference....
Use a database; have a table which logs the ip and timestamp then lookup the ip on every request and check the time of the last request is more than 5 minutes ago
Use a file to store the request ip and timestamp in something like CSV or JSON format. Then loop over the file with each request...
Seeing as you don't say anything about access to a database, here is a version for a JSON formatted file:
Note: you need to update the $filePath variable
$ip = $_SERVER["REMOTE_ADDR"];
$filePath = "./path/to/file.json"; // Set this to a file on your server
$ipList = json_decode(file_get_contents($filePath), true);
$throttleTime = date("Y-m-d H:i:s", strtotime("-5 minutes");
$allowAccess = true;
foreach ($ipList as $key => $row) {
// Check if the IP has accessed the site in the last 5 minutes
if ($row["ip"] === $ip && $row["timestamp"] > $throttleTime)) {
$allowAccess = false;
}
// Remove the entry if over 5 minutes old; not restricted to IP
// >> Garbage colection
if ($row["timestamp"] < $throttleTime) {
unset($ipList[$key]);
}
}
$ipList[] = [
"ip" => $ip,
"timestamp" => date("Y-m-d H:i:s"),
];
// Update the
file_put_contents($filePath, json_encode(array_values($ipList)));
// Check to see if the user is allowed access; exit with error message if not.
if (!$allowAccess) {
echo "Error, you've accessed this site too many times. Try again in 5 minutes.";
exit;
}
// The user is allowed... Do something....
echo "We're in!";

Generate file JSON from PHP script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
How can I generate this structure file JSON from PHP script?
Example JSON:
{
"messages":[
{
"attachment":{
"type":"image",
"payload":{
"url":"#"
}
}
}
]
}
Thanks :)
That should work.
$a = ["messages" => [["attachment" => ["type" => "image", "payload" => ["url" => "#"]]]]];
$b = json_encode ($a);

I am working on IP create in form and want explode some data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
It's my store method where I am getting IP through request from my view
public function store(Request $request)
{
$ip['ip_range'] = $request->input('ip_range');
$ip['min_ip'] = $request->input('ip_range');
$ip['max_ip'] = $request->input('ip_range');
$ip_data = Ip::create($ip);
session()->flash('msg', ' Successfully created');
return view('ip.index');
}
In above I want if IP comes as
192.168.2.9
192.168.1.2
It should be saved as 2 values .... Mean it should separate it on /r/n and where I found /n I create a new value and so on.
And next thing is that if user enter as
192.168.1.0-3
Here a - comes at last one 192.168.1-3 should be save in ip_range and 192.168.1.0 in min_ip while 192.168.1.3 as max_ip.
I hope this is what you were looking for:
public function store(Request $request) {
$ip['ip_range'] = $request->input('ip_range');
$arrIps = explode("\r\n", $ip['ip_range']);
foreach($arrIps as $strIp) {
$arrRange=explode("-",$strIp);
if(count($arrRange)>1){
$ip['min_ip']= $arrRange[0];
$arrDot=explode(".",$arrRange[0]);
$arrDot[3]=$arrRange[1];
$ip['max_ip']= implode(".",$arrDot);
}
else
$ip['min_ip']=$ip['max_ip']="";
$ip_data = Ip::create($ip);
}
session()->flash('msg', ' Successfully created');
return view('ip.index');
}

JSON encode in PHP and MySql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I wanted this type of json using php and mysql. I tried a lot but the json which I get are duplicate data and very complex.
JSON I need:
{"data":[
{"Maharashtra":[
{"Mumbai":[
{"place_name":"Gateway of India"},
{"place_name":"Marine Lines"},
{"place_name":"Juhu"}
]},
{"Pune":[
{"place_name":"Singhad"},
]}
}],
{"Goa":[
{"Panji":[
{"place_name":"panji"}
]}
]}
}]}
PHP code:
<?php
require('database.php');
$counter=0;
$state = "SELECT distinct(s.state_name)
, ac.city_name
FROM all_state s, all_city ac
WHERE s.s_id = ac.state_id;";
$resultState = $conn->query($state);
$return_arr['data'] = array();
while($row = $resultState->fetch_assoc()){
$getStateName = $row['state_name'];
$getCityName = $row['city_name'];
$state_array[$getStateName] = array($getCityName);
array_push($return_arr['data'], $state_array);
}
echo json_encode($return_arr);
your query only returns states and cities so data like:
{"place_name":"Gateway of India"},
{"place_name":"Marine Lines"},
{"place_name":"Juhu"}
is not there: to push the data you are getting in to propper array use this:
while($row = $resultState->fetch_assoc()){
$stateArray[$row['state']][$row['city']] = array(); // the empty array is there to push your remaining data in to
}
$result = array('data' => $stateArray);
echo json_encode($result);
UPDATE TO ANSWER COMMENT:
You can add details to your array like this (or any other way, there are many, this is the most basic):
$stateArray[$stateYouWant][$cityYouWant]['place_names'] = array('Gateway of India', 'Marine Lines', 'Juhu');
This will give ytou something like:
{"Maharashtra":[
{"Mumbai":[
{"place_names":[
"Gateway of India",
"Marine Lines",
"Juhu"]}
]},
The variation you want:
{"Mumbai":[
{"place_name":"Gateway of India"},
{"place_name":"Marine Lines"},
{"place_name":"Juhu"}
]},
Is not possible because json is actually a string representation of associative array (or JS object) and that means you can not have same name keys on the same array level.

Cause of "Method name must be a string" [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
public function run() {
$this->step = $this->$_POST("step", 1);
$this->xml = new XMLFile();
$common_data = array(
'STEPCONTENT' => $this->get_step_content(),
'STEPNUMBER' => $this->step,
'STEPTITLE' => $this->get_step_title()
);
echo $this->parse($this->common_template, $common_data);
This gives the exception:
Fatal error: Method name must be a string in
C:\xampp\htdocs\test\openad\install\InstallOpenAdServer.php on line 674
Why?
This is the culprit
$this->step = $this->$_POST("step", 1);
You cannot use an $_POST super global array as a function. If you are trying to access the vairable from the $_POST, you can simple do this
$this->step = $_POST["step"];

Categories