I have three HTML form field values (name, saywords, mail) that I try to concat in php and write into one single txt file on my server:
Each value should be on a new line in the txt field, as is why I added the "\n" in the array .... where's my error?
Thanks a lot for any help!
$name = $_POST['name'];
$saywords = $_POST['saywords'];
$mail = $_POST['mail'];
$data = array($name, . "\n" $mail, . "\n" $saywords);
file_put_contents("$t.$ip.txt",$data); // Will put the text to file
You have two problems:
$data should be a string not an array
. concatenates the left hand side and the right hand side: "abc" . "def" becomes "abcdef".
putting the dot first like in . "\n" or even . "\n" $mail doesn't make sense in PHP so you'll get a parse error.
Replace your $data = line with $data = $name . "\n" . $mail . "\n" . $saywords; and you'll be good to go.
i don't see the use of array , you can concatenate them just like :
$data = $name . "\n" . $mail . "\n" . $saywords ;
It depends with the operating system of the server.
Try "\r\n" instead of "\n"
Okay, so here is my shot.
/*first of all, you should always check if posted vars are actually set
and not empty. for that, you can use an universal function "empty", which
checks if the variable is set / not null / not an empty string / not 0.
In such way you will avoid PHP warnings, when some of these variables
will not be set*/
$name = !empty($_POST['name']) ? $_POST['name'] : '';
$saywords = !empty($_POST['saywords']) ? : $_POST['saywords'] : '';;
$mail = !empty($_POST['mail']) ? $_POST['mail'] : '';
/*Secondly, do not use \n, \r, \r\n, because these are platform specific.
Use PHP_EOL constant, it will do the job perfectly, by choosing
what type of line-break to use best.
As others mentioned - in your scenario, the string would be better solution.
Add everything into string, and then put its contents into file. Avoid using
double quotes, when you define PHP strings, and use single quotes instead - for
performance and cleaner code.
*/
$data = 'Name: '.$name.PHP_EOL.'E-Mail: '.$mail.PHP_EOL.'Message: '.$saywords.PHP_EOL.PHP_EOL;
file_put_contents($t.$ip.'.txt', $data); // Will put the text to file
By the way, I strongly suggest to also add some extra validation, before saving data to that txt file. With this code somebody can easily mess up contents of your txt file, by posting huge amounts of data with no limits.
Tips:
1) Accept only Names with limited lengths and charaters (do not allow to use special symbols or line breaks - you can also filter them out, before saving)
2) Validate e-mail which has been entered - if it is in correct format, does mx records exists for the domain of e-mail address, and so on...
3) Accept "saywords" with limited length, and if needed - deny or filter out special characters.
You will get much cleaner submissions by doing this way.
Use <br /> as you use html code
Related
Ok, so right now I've got this URL, which works perfectly:
$html_string = file_get_contents('https://www.123.com/' . $_GET["ticket"]);
What I want to do is to insert $_GET between the URL.
Here's what I mean:
('https://www.123.com/' . $_GET["ticket"] /url-continues-here);
I have tried everything, but can't find any solution how to do it without an error.
Concatenate the 2 strings first and after that use file_get_contents()
$html_string = 'https://www.123.com/' . $_GET["ticket"] . '/url-continues-here';
file_get_content($html_string);
I'm sorry that this is basic. When I use this PHP code it works fine:
$data = '{"reportID":1092480021}';
However, when I run my URL like this:
http://localhost:8000/new/reportget.php?type=1092480021
and use this PHP code:
$reportref = $_GET['type'];
$data = '{"reportID:".$reportref."}"';
I get the error
Error_description:reportID is required
I think it's an error with how I am joining my variable to the string but I can't understand where I am going wrong.
Your string is improperly quoted. To match the format in your first example use:
$data = '{"reportID":' . $reportref.'}';
Note there are no double quotes on the last curly.
Even better:
$reportref = 1092480021;
$data = [ 'reportId' => $reportref ];
var_dump(json_encode($data));
Output:
string(23) "{"reportId":1092480021}"
For simple view and understanding, can you try out:
$data = "{\"reportID\":$reportref}";
Think that should sort it out
Use it like this
data = '{"reportID:"'.$reportref.'"}"';
It isn't working because you wrap all the value within single quote and when it come to concatenate the $reprtref you put directly .$reportref without closing the first single quote and after putting the value to concatenate you forget to open another single quote
'{"reportID:".$reportref."}"';
the correct value is
'{"reportID:"' . $reportref . '"}"';
and to match the way you specify your $data value It must be like this
'{"reportID":' . $reportref . '}';
We are currently performing searches on a Database and returning results in JSON format to use on a Google Maps. The file that we call is named getvenues.php and works great on the server. It accepts a number of parameters and returns the results based on the query.
We then have a separate file that checks to see if there's a JSON file on the server which contains the results, matches its age against a setting, and then returns the data either from the cache file, or builds a new cache file if it's too old.
Since there are several thousand possible search options, we only cache single searches (either on a County, Region or Type). The JavaScript always calls our search_cache_builder.php file. If there is more than one search parameter, the file simply gets the contents returned by getvenues.php and serves it up without any caching.
Everything works great except for one particular combination. If a search is run for venue_type=Castle / Fort and venue_name=Leeds Castle, the search_cache_builder.php returns an empty array, even though accessing getvenues.php directly returns the required data.
Here's a sample of the getvenues.php working with this data > http://dev.weddingvenues.com/lincweb/getvenues.php?type=Castle%20/%20Fort&venue_name=Leeds%20Castle
And here's what the search_cache_builder.php script returns with an identical search (the address we are sending to is correct) > http://www.weddingvenues.com/search_cache_builder.php?type=castle%20/%20fort&venue_name=Leeds%20Castle
Here's the code for the search_cache_builder.php file, which relates to this particular query:
$get_string = '?';
foreach($_GET as $key => $value)
{
if($get_string === '?')
{
$get_string .= $key . '=' . $value;
}
else
{
$get_string .= '&' . $key . '=' . $value;
}
}
//$get_string = str_replace(' ', '', $get_string);
// Otherwise, we need to serve up the page as is:
$file_url = GET_VEN_URL . $get_string;
echo file_get_contents($file_url);
Can anyone offer an explanation as to why the search_cache_builder.php file is returning an empty array?
You should urlencode() your parameter values.
In fact, while your getvenues.php receives parameters directly from a browser it behaves OK, 'cause they are correctly urlencoded.
I tried what follows in my computer towards your service and it works:
define ("GET_VEN_URL", "http://www.weddingvenues.com/getvenues.php");
$get_string = '?';
foreach($_GET as $key => $value)
{
if($get_string === '?')
{
$get_string .= $key . '=' . urlencode($value);
}
else
{
$get_string .= '&' . $key . '=' . urlencode($value);
}
}
$file_url = GET_VEN_URL . $get_string;
echo "<pre>";
echo $file_url;
echo "<pre>";
echo file_get_contents($file_url);
Because $get_string === '?' is ALWAYS false, change it to ==, ? is not a boolean.
I would not recommend using GET parameters, &,? as part of the file name.
Beside this, it will quickly hit into 5k (or 4k, can not recall) limit for a file name.
You can do a sort by $GET, md5 (or hash) the array to a random string.
As long you ensure the hashing mechanism is consistent, you can easily retrieve the cache file.
First try to urldecode values, then use http_build_query to generate your get_string, your problem must be the special chars in values (like /).
Edit:
If you change the order it works: http://www.weddingvenues.com/search_cache_builder.php?venue_name=Leeds%20Castle&type=castle%20/%20fort
Simple requirement: I want to store a flat unchanged XML strings into a MySQL-DB and then fetch the string itself via php with the tags (i.e. <tag>1234</tag>).
Problem: When I fetch the string, I get the values not the entire XML string.
I store <tag>1243</tag> (done via PHPmyAdmin) then I fetch (see code below) and echo the result I get 1234 not <tag>1234</tag>.
$query = "SELECT * FROM " . $my_table_with_flat_xml_string;
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$xml_flat_file_with_the_tags_please = $row[0];
echo " ( " . $tags. ")";
}
Help!
The String fetched from the database is <tag>1234</tag> as you put it into the database. MySQL does not interpret or parse XML input as long as you don't tell it to.
But you only echo it without escaping the < and >-chars, so your browser thinks, this would be a HTML-Element.
You can use something like the var_dump()-function to print out your fetched value or you escape the brackets by using the htmlspecialchars()-function.
Every node has a "name" (e.g. "tag") and a value (e.g. "1234", or perhaps "empty").
You've got the value.
You simply want to store the name, too. Twice.
By creating the string "<" . $name . ">" . $value . ""
There are several different ways to get $name and $value - it all depends on exactly how you're parsing your XML file.
Here's an excellent tutorial that gives you details on using XML from PHP:
http://www.ibm.com/developerworks/xml/library/x-xmlphp1/index.html
http://www.ibm.com/developerworks/xml/library/x-xmlphp2/index.html
http://www.ibm.com/developerworks/xml/library/x-xmlphp3/index.html
I'm using the following code but it gives me an error about an unexpeted " in the statement at this line, but I'm not sure how the syntax should go:
list($width,$height,$type,$attr) = getimagesize("' . $SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo'] . '");
echo "<p>This logo is ".$width; x $.height; echo "pixels in size.</p>";
PHP says the error is on that first line.
Try this
list($width,$height,$type,$attr) = getimagesize($SESSION_['html_folder'] . '/uploadedfiles/' . $row['logo']);
The error in your code was that you opened the string "twice", one with " and one with '. If you use a variable as parameter where a string is expected, you do not need to set the variable in quotes.
You should make sure that in $SESSION_['html_folder'] there's no malicious code, e.g. with
if(!is_dir($SESSION_['html_folder']))
die("ERROR");
youre adding all kinds of unnecessary quotes
list($width,$height,$type,$attr) = getimagesize($SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo']);
You want:
list($width,$height,$type,$attr) = getimagesize($_SESSION["html_folder"] . '/uploadedfiles/' . $row['logo']);
The main issue was that you were using double quotes to open a string, but you didn't close the string with double quotes. The above is a better formated string(and fixed your $_SESSION variable).