I think I'm going crazy.
code:
<?
$meh = $_GET["q"];
echo ( ":" . $meh . ":" . strlen($meh) . PHP_EOL );
$fp = fopen("/tmp/wtf.log","w+");
fwrite($fp, ":" . $meh . ":" . strlen($meh) . PHP_EOL );
fclose($fp);
?>
request:
/search.php?q=meh123
This is in the response ( expected ):
:meh123:6
This is in the file:
me#host:/tmp# cat wtf.log
::0
Try this:
<?
$meh = $_GET["q"];
$writeline = ":{$meh}:{strlen($meh)}";
echo ( $writeline );
$fp = fopen("/tmp/wtf.log","w+");
fwrite($fp, $writeline );
fclose($fp);
?>
Also, as Jay said in the comment above:
try putting if(isset($_GET['q'])) { // code } around your code (something you should be doing anyway)
Related
I've tried everything I could think of yet I still couldn't get the code to delete the new line character that is appended at each new entry.
if ($_POST['what_to_do']==1){
if ((isset($_POST['chat_name'])) and ($_POST['chat_name']!=NULL)){
$chat_name=$_POST['chat_name'];
$myfile = fopen("users.txt", "a");
fwrite($myfile, "user_id" . $user_id . " " . "chat_name" . $chat_name . ";\n");
fclose($myfile);
$_SESSION['is_chat_logged'] = 1;
}
}
elseif ($_POST['what_to_do']==2){
if ($_SESSION['is_chat_logged'] = 1){
$actual_file=file_get_contents("users.txt");
$position_start=strpos($actual_file, "user_id" . $user_id . " ");
$line=file_get_contents("users.txt",FALSE,NULL,$position_start);
$position_end=strpos($line, ";")+1+$position_start;
$part_to_be_replaced=substr($actual_file, $position_start, $position_end-strlen($actual_file));
$new_actual_file= str_replace($part_to_be_replaced, "", $actual_file);
$myfile = fopen("users.txt", "w");
fwrite($myfile, $new_actual_file);
fclose($myfile);
$_SESSION['is_chat_logged'] = 0;
}
}
The users.txt looks like this:
user_id1 chat_nameJake;
user_id2 chat_nameSomeone;
user_id43 chat_nameZeke;
user_id22 chat_nameBilly;
Any help would be appreciated.
EDIT: I've found out what the problem was. I was using the substr() function the wrong way. Instead of $position_end-strlen($actual_file) I should have put $position_end-$position_start+1
change
fwrite($myfile, "user_id" . $user_id . " " . "chat_name" . $chat_name . ";\n");
to
fwrite($myfile, "user_id" . $user_id . " " . "chat_name" . $chat_name . ";");
The \n represents a new line.
notice when you are writing file (fwrite) you are appending a "/n" char which is a new line char. in order to remove new line char you just need to remove that "/n" from your fwrite function
This question already has answers here:
Turn off warnings and errors on PHP and MySQL
(6 answers)
Closed 5 years ago.
It prints on the screen when it is correctly entered, but I do not want to do anything when it is entered incorrectly
How can I do that?
<?php
header('Content-type: text/html; charset=utf8');
$api_key = 'local';
$keyword = 'test';
$url = 'test.json' . $api_key . '&' .'keyword' .'=' . $GLOBALS['q'] ;
$open = file_get_contents($url);
$data = json_decode($open, true);
$istatistikler = $data['data'];
if ($data) {
foreach ( $istatistikler as $istatistik ){
echo '<div class="right">';
echo 'Oyun modu: ' . $istatistik['title'] . '<br />' .
'Kazanma: ' . $istatistik['content'] . '<br />' .
'Kazanma: ' . $istatistik['image'] . '<br />' .
'Kazanma: ' . $istatistik['category'] . '<br />' .
'<br />' .
'<hr/>';
$karakter_simge = 'http://google.com' . $istatistik['image'] . '';
echo "<img src=".$karakter_simge." >" ;
echo '</div>';
}
}
?>
Successful output
Failed output
Warning:
file_get_contents(http://localhost/api/detail?X-Api-Key=local&keyword=a):
failed to open stream: HTTP request failed! HTTP/1.1 406 Not
Acceptable in /opt/lampp/htdocs/weather-master/php/php-api.php on line
10
"I do not want to print unsuccessfully"
thank you for your help!
This may be helpful:
$open = #file_get_contents($url);
# sign before a function name (in a call) prevents from showing any warnings (It's a bad practice though).
Good luck!
Change
$open = file_get_contents($url);
into
$open = #file_get_contents($url);
if ($open === false)
die("wrong");
The # suppresses the error message. Using die() will abort the script completely with the given message.
Alternatively, change the condition to !== false and wrap the rest of your "successful" code in its body:
$open = #file_get_contents($url);
if ($open !== false)
{
$data = json_decode...
...
...
}
I guess I overshot the goal here a little, but not even running into code that won't work properly without its data isn't a bad idea at all.
I have got this code to open each file in a folder, decrease a value by 5 and then overwrite the existing content.
<?php
foreach (glob("users/*.php") as $filename) {
$array = file($filename);
$wallet = $array[0];
$time = $array[1];
$status = $array[2];
$steamid = $array[3];
$process = fopen($filename, "w") or die("Unable to open file!");
$time = $time - 5;
$newdata = $wallet . "\n" . $time . "\n" . $status . "\n" . $steamid;
fwrite($process, $newdata);
fclose($process);
}
?>
Before I execute the script, the files that are opened look like this:
680
310
0
74892748232
After the script was executed, the file looks like this:
680
305
0
74892748232
If the script is executed again, it adds more lines and just breaks the files even more.
The string for the file is created here:
$newdata = $wallet . "\n" . $time . "\n" . $status . "\n" . $steamid;
Why does it add an empty line after $wallet and $status, but not after $time? How can I avoid this and instead write down:
$wallet
$time
$status
$steamid
Thanks a lot in advance.:)
Try with this solution
You can also use file_put_contents():
file_put_contents($filename, implode("\n", $array) . "\n", FILE_APPEND);
from this SO question
https://stackoverflow.com/a/3066811/1301180
$np = $_POST["np"]; // `sky` for example
$a = "inc/" . $np . ".php";
$new = fopen($a, "w") or die("Unable to open file!");
$str="$np='" . $np . "';";
fwrite($new, $str);
fclose($new);
I need to create a new inc/.sky.php file and write the following inside it:
$np = 'sky';
But what I get is:
sky='sky';
Any idea?
This should work for you:
$np = $_POST["np"]; // `sky` for example
$a = "inc/" . $np . ".php";
$new = fopen($a, "w") or die("Unable to open file!");
$str='$np = \'' . $np . "';";
fwrite($new, $str);
fclose($new);
The problem was that you are using " to define the string $str. Therefore, the content of $np was printed instead of the string '$np'.
Use single comma's
This will insert the $np variable into your sting
$str="$np='" . $np . "';";
This will insert np as text
$str='$np=\'' . $np . '\';';
I want to dump request variables to a file for debugging. How's this possible?
<?php
$req_dump = print_r($_REQUEST, TRUE);
$fp = fopen('request.log', 'a');
fwrite($fp, $req_dump);
fclose($fp);
Untested but should do the job, just change request.log to the file you want to write to.
I think nowadays this method is easier and faster:
$req_dump = print_r($_REQUEST, true);
$fp = file_put_contents('request.log', $req_dump, FILE_APPEND);
Use serialize() function for dumping. Dump $_SERVER, $_COOKIE, $_POST and $_GET separately (may go to the same file). If you're planning on debugging with the data it helps to know if the data was part of a POST request or a GET request.
Dumping everything is good for debugging in development, but not so in production. If your application does not have many users, it can work in production too. If you anticipate many users, consider dumping just the $_POST data, or limit server variables to those starting with HTTP_.
/* may be late but he can help others.
it's not my code, I get it from :
https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
*/
class DumpHTTPRequestToFile {
public function execute($targetFile) {
$data = sprintf(
"%s %s %s\n\nHTTP headers:\n",
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI'],
$_SERVER['SERVER_PROTOCOL']
);
foreach ($this->getHeaderList() as $name => $value) {
$data .= $name . ': ' . $value . "\n";
}
$data .= "\nRequest body:\n";
file_put_contents(
$targetFile,
$data . file_get_contents('php://input') . "\n"
);
echo("Done!\n\n");
}
private function getHeaderList() {
$headerList = [];
foreach ($_SERVER as $name => $value) {
if (preg_match('/^HTTP_/',$name)) {
// convert HTTP_HEADER_NAME to Header-Name
$name = strtr(substr($name,5),'_',' ');
$name = ucwords(strtolower($name));
$name = strtr($name,' ','-');
// add to list
$headerList[$name] = $value;
}
}
return $headerList;
}
}
(new DumpHTTPRequestToFile)->execute('./dumprequest.txt');
// add this line at the end to create a file for each request with timestamp
$date = new DateTime();
rename("dumprequest.txt", "dumprequest" . $date->format('Y-m-d H:i:sP') . ".txt");
<?php //log
$razdelitel = '--------------------------------------------'.PHP_EOL . date("Y-m-d H:i:s") .PHP_EOL.PHP_EOL;
$data_REQUEST = '$_REQUEST: ' . print_r($_REQUEST, true).PHP_EOL;
$data_POST = '$_POST: ' . print_r($_POST, true).PHP_EOL;
$data_GET = '$_GET: ' . print_r($_GET, true).PHP_EOL;
$data_all = $razdelitel . $data_REQUEST . $data_POST . $data_GET;
$name_txt = __DIR__ . '/log_' . date('m.Y') . '.txt'; //log_12.2021.txt
$chmod = '0244';
chmod($name_txt, $chmod);
file_put_contents($name_txt, $data_all, FILE_APPEND);
//var_dump($name_txt, $chmod); ?>