I am currently building a form that sends a user off to an external payment site at a certain point during the form.
At this point I have provided two URLs to the service that is providing this payment service for me, one to call the function that will update a users record at the point of pyment, and a URL that a user is directed back to after the payment process is complete.
The first URL (function call to update a users record) is "out-of-band" and will never be displayed to the user and will be from the server rather than client IP. Because of this I am in a bit of a 'blind-alley' as I cannot see the variables that are being passed back to me, and obviously these are important in order for me to parse and manipulate this data (post) in order to update our records.
I have tried writing a simple algorithm inside my function that would loop through these POST variables and write them to a text file but unfortunately I couldn't get this working (maybe something to do with Joomla?).
Here is that function:
public function harlandReturn() {
$myFile = "text.txt";
$fh = fopen($myFile, 'w');
$stringData = "";
foreach($_POST as $i => $v) {
$stringData .= $i . " : " . $v . " / ";
}
fwrite($fh, $stringData);
fclose($fh);
}
My main question is, is there a way i can get a function to log and store any data sent to it in a file, email or something similar so I can see what they are (ints, strings, boolean, etc) and their names?
Thanks
Because variables aren't always predictable - for instance, subarrays - you're probably best serializing the array and writing that to the file.
if(is_writable($myFile)) {
$fh = fopen($myFile, 'w');
$stringData = serialize($_POST);
fwrite($fh, $stringData);
fclose($fh);
}
You can then either open the file directly or create a quick script using unserialize to view it as POSTed.
Note that you're not doing any error checking to see if the file is writable, etc.
your function is missing fwrite()
$myFile = "text.txt";
$fh = fopen($myFile, 'w');
foreach($_POST as $i => $v) {
$stringData = $i . " : " . $v . " / ";
fwrite($fh, $stringData);
}
fclose($fh);
or
$myFile = "text.txt";
$fh = fopen($myFile, 'w');
foreach($_POST as $i => $v) {
$stringData .= $i . " : " . $v . " / ";
}
fwrite($fh, $stringData);
fclose($fh);
Related
I am trying to make a program where I can add things to a list, read things, and clear the list. I have the clear function working perfectly, however I can't seem to add or read more than 1 line at a time. I am using fwrite($handle, $MyString); but that replaces everything in the entire file with $MyString. To get the information from the file I am using $list = fgets($handle); and then using echo to print it. This reads the first line in the file only.
Any help?
Thanks!
Getlist code:
<?php
$myFile = "needlist.txt";
$fh = fopen($myFile, 'r');
$theData = fgets($fh);
fclose($fh);
echo $theData;
?>
Add to the list code:
<?php
$neededlist = "needlist.txt";
$fh = fopen($neededlist, 'w');
$user_message = $_REQUEST['txtweb-message'];
$needed .= $user_message;
$needed .= "\n";
fwrite($fh, $needed);
fclose($fh);
echo "You have successfully added ", $user_message;
?>
When you write to the file are you opening your filehandle with the "a" mode option? Opening with "w" or "x" truncates it so you start with a clean file (http://php.net/fopen)
fgets(); reads only until the end of the line ( http://php.net/fgets ). To get the whole file you can try:
var $list = "";
var $line = "";
while ($line = fgets($handle)) {
$list = $list . "\n" . $line;
}
echo $list;
You want to add the "\n" because fread doesn't read the linefeeds IIRC. There're also a couple functions that might be more appropriate in this situation like file_get_contents and fread.
Fgets returns only one string. You should use it in cycle like that:
while (($buffer = fgets($handle, 4096)) !== false) {
echo $buffer;
}
Hi I need to receive a post data which will be in an xml that is encoded in a base64 format.
I'll be receiving this from a payment gateway. Now all i get is this. My code creates a txt file but is empty. Is there anything wrong with the code? The output should be an xml envelope in a text file.
$body = '';
$fh = #fopen('php://input', 'r');
if ($fh)
{
while (!feof($fh))
{
$s = fread($fh, 1024);
echo $s;
if (is_string($s))
{
$body .= $s;
}
}
fclose($fh);
}
$body = base64_decode($body);
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $body;
fwrite($fh, $stringData);
fclose($fh);
I tried to contact the payment gateway and they are telling me that they are getting this error "The remote server returned an error: (417) Expectation failed." where could the problem exist us or them?
Since your file is returning blank, I would recommend verifying the specifications from the payment gateway for your fopen() function. In addition, if you are properly getting data back from them, then I would check the base64_decode() function. I have seen situations where there may be a header or other data at the top of the actual payload data that fouls up the base64_decode and ruins your day.
It looks like you are mixing up your file handlers, can you see if the following codes run:
$body = $HTTP_RAW_POST_DATA;
$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
$stringData = $body;
fwrite($ourFileHandle, $stringData);
fclose($ourFileHandle);
I have just tested the above code myself successfully.
What I suggest you do is trying the following:
Try to put a static piece of data into $body (eg: "$body = 'test';")- and see if that saves - if not then it is an issue at your end.
Delete the file (to remove any permission issues).
Double check the url that the payment gateway is sending to is correct.
I have an issue with writing and reading to text file.
I have to first write from a text file to another text file some values which I need to read again. Below are the code snippets:
Write to text file:
$fp = #fopen ("text1.txt", "r");
$fh = #fopen("text2.txt", 'a+');
if ($fp) {
//for each line in file
while(!feof($fp)) {
//push lines into array
$thisline = fgets($fp);
$thisline1 = trim($thisline);
$stringData = $thisline1. "\r\n";
fwrite($fh, $stringData);
fwrite($fh, "test");
}
}
fclose($fp);
fclose($fh);
Read from the written textfile
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
echo rtrim($kw[$i]);
}
But, if I am not mistaken due to the "/r/n" I used to insert the newline, when I am reading back, there are issues and I need to pass the read values from only the even lines to a function to perform other operations.
How do I resolve this issue? Basically, I need to write certain values to a textfile and then read only the values from the even lines.
I'm not sure whether you have issues with the even line numbers or with reading the file back in.
Here is the solution for the even line numbers.
$page = join("",file("text2.txt"));
$kw = explode("\n", $page);
for($i=0;$i<count($kw);$i++){
$myValue = rtrim($kw[$i]);
if(i % 2 == 0)
{
echo $myValue;
}
}
I'm writing a script for a lead contact form that needs to send the first 10 leads to email 1, the second 10 leads to email 2, and so on until it gets to email 4, and then it goes back to email 1.
this is a rotator i have that was built for landing pages, but it rotates 1 each time rather than waiting 10 times, and then rotating. how would I modify this to suit my needs?
Also, it cant happen on every 'refresh' obviously. there would need to be a separate group of code which would go in the action="whatever.php" of the form and thats the code that would increment it.
<?php
//these are the email addresses to be rotated
$email_address[1] = 'email1#email.com';
$email_address[2] = 'email2#email.com';
$email_address[3] = 'email3#email.com';
$email_address[4] = 'email4#email.com';
//this is the text file, which will be stored in the same directory as this file,
//count.txt needs to be CHMOD to 777, full privileges, to read and write to it.
$myFile = "count.txt";
//open the txt file
$fh = #fopen($myFile, 'r');
$email_number = #fread($fh, 5);
#fclose($fh);
//see which landing page is next in line to be shown.
if ($email_number >= count($email_address)) {
$email_number = 1;
} else {
$email_number = $email_number + 1;
}
//write to the txt file.
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $email_number . "\n";
fwrite($fh, $stringData);
fclose($fh);
//include the landing page
echo $email_address[$email_number];
//terminate script
die();
?>
What I understood from your question is there would be form to submit leads and when a lead is submitted, it has to follow your logic. Correct me if I'm wrong.
If that be the case,
use two a text file like track.txt. The initial contents of this text file would be 1,0. Which means leads are sent to first email id 0 times.
So in the action script of the form include the following code.
<?php
$email_address[1] = 'email1#email.com';
$email_address[2] = 'email2#email.com';
$email_address[3] = 'email3#email.com';
$email_address[4] = 'email4#email.com';
$myFile = "track.txt";
//open the txt file
$fh = #fopen($myFile, 'r');
$track = #fread($fh, 5);
#fclose($fh);
$track = explode(",",$track);
$email = $track[0];
$count = $track[1];
if($count >= 10)
{
$count=0;
if($email >= count($email_address))
{
$email = 1;
}
else
{
$email++;
}
}
else
{
$count++;
}
$track = $email.",".$count;
//write to the txt file.
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $track);
fclose($fh);
//send lead to $email
?>
I have a little issue while trying to kill time. I am writint to a text file the contents of the filled in form so it is easy to read. At the moment everything gets put int a single line, and therfore I can not tell where msgs begin or end. I wrote a php like this:
$from = $_POST[from];
$friend = $_POST[friend];
$carrier = $_POST[carrier];
$message = stripslashes($_POST[message]);
if ((empty($from)) || (empty($friend)) || (empty($message))) {
header ("Location: sms_error.php");
}
else if ($carrier == "orange_mobile_network") {
$formatted_number = $friend."#orange_mobile_network.co.uk";
mail("$formatted_number", "SMS", "$message");
header ("Location: sms_success.php");
}
Then the SMS/Text message will be sent. After this I wanted to write/store/append the message on a txt file. So I wrote:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
$stringData = $_POST[from];
fwrite($fh, $stringData);
$stringData = $_POST[friend];
fwrite($fh, $stringData);
$stringData = $_POST[message];
fwrite($fh, $stringData);
fclose($fh);
But like I said. Everything works. I just want to be able to read the file and put everything in a new line and possibly format it nicely for easy reading. I do not want to use a DB for storing the TXT as my host is going to charge me.
change this:
fwrite($fh, $stringData);
with this:
fwrite($fh, $stringData . PHP_EOL);
or:
fwrite($fh, $stringData . "\r\n");
You can append PHP_EOL when you write for a platform aware newline. I.e. $stringData = <string> . PHP_EOL;
If don't need it to be platform aware, going with the windows newline is a safe option.
define('CRLF', "\r\n");
$stringData = <string> . CRLF;
fwrite($fh, $stringData);
I do not know exactly Cause you're using. txt instead of a bank, but my tip for this specific case is to work with json format.
Example:
$myFile = "sms_Numbers_Mgs.txt";
$fh = fopen($myFile, 'a+') or die("can't open file");
//assembles a string of type json
$data = json_encode(array(
'from' => $_POST[from],
'friend' => $_POST[friend],
'message' => $_POST[message]
));
//Write in the file
fwrite($fh, $data);
fclose($fh);
//reads the file
$content = file_get_contents($myFile);
$object = json_decode($content);
var_dump($object);
good it is at least better organized.
a hug!