0

I am trying to save the $_Get, user:pass to text file in my website.

I am sorry but am not a php dev. so i've come with following code:

<?php
   if( $_GET["user"] || $_GET["pass"] ) {
        $file = 'somefile.txt';
        file_put_contents($file, $_GET["user"] . PHP_EOL, FILE_APPEND);
        file_put_contents($file, $_GET["pass"] . PHP_EOL, FILE_APPEND);
   }
?>

Now it save

User
pass

How to save it as

User:pass

Thank you, regrads

mza box
  • 67
  • 1
  • 3
  • 12

1 Answers1

1

You simply need to create a string in the required format. For example

file_put_contents($file, "$_GET[user]:$_GET[pass]" . PHP_EOL, FILE_APPEND);

Note: within a double-quoted string, associative array indexes do not need to be quoted.

See here for an example ~ https://3v4l.org/Fr30Y


Your script can trigger potential E_NOTICE level errors around undefined indexes so if you want to play it safe, I would make sure both user and pass indexes exist by changing

if( $_GET["user"] || $_GET["pass"] ) {

to

if ( isset($_GET['user'], $_GET['pass']) ) {
Phil
  • 157,677
  • 23
  • 242
  • 245
  • I think you may need curly braces around the variables, but maybe there are cases where you don't. – C Miller Dec 27 '17 at 03:20
  • @CMiller nope, not in this case – Phil Dec 27 '17 at 03:22
  • The only thing you would want to look out for is if there is a defined keyword that matches the array key. If so it will replace the key name with the key word. The way to prevent that would be "{$_GET['user']}:{$_GET['pass']}". – C Miller Dec 27 '17 at 03:26
  • @CMiller that is completely unnecessary. I suggest you read the manual. See http://php.net/manual/language.types.string.php#example-52 – Phil Dec 27 '17 at 03:27
  • I just tested it by defining a keyword and the keyword replaces the array key. There is even a question covering it on this site: https://stackoverflow.com/questions/2405482/is-it-okay-to-use-arraykey-in-php – C Miller Dec 27 '17 at 03:30
  • @CMiller did you test it within a double-quoted string? I bet you didn't – Phil Dec 27 '17 at 03:31
  • @CMiller on that question, [this answer](https://stackoverflow.com/a/5059548/283366) mentions the same difference as I have – Phil Dec 27 '17 at 03:45
  • You are correct. keywords don't work in double quotes. – C Miller Dec 27 '17 at 03:47
  • @phil I have a small question, do you mind to know how to decode_base64 now the $_Get[1], $_Get[2] ? – mza box Dec 27 '17 at 04:55
  • @mzabox really not sure what you mean but PHP has the [`base64_decode()`](http://php.net/manual/function.base64-decode.php) function – Phil Dec 27 '17 at 05:24