1

I have an fwrite() function to create an email activation link. I need to use cookies for that link. First I enter:

if ($filecheck == "F") {
$file = $getuseremailresult['username'] . rand() . rand() . ".php";
$filename = "../" . $file;
$link = "http://new.connectionsocial.com/users/LoginRegisterName/" . $file;
$handle = fopen($file, "w") or die("<h1 style='text-align: center; color: red;'>There has been an error creating the activation link. Please try again later.</h1>");
$content = "

include_once('ConnectionDB.php');

$cookie = sha1($_COOKIE['TemporaryCookie']);

?>
";
fwrite($handle, $content);
mysqli_query($connection, "UPDATE userfilescheck SET FileCreated='T'");
}

And everything is fine so far. However, when I add in this to $content:

$cookie = sha1($_COOKIE['TemporaryCookie'];

The entire page literally disappears, and none of the code works. Am I supposed to escape part of the $cookie value, or is it just a parsing error?

Programmer Dude
  • 497
  • 1
  • 4
  • 15
  • possible duplicate of [PHP Parse/Syntax Errors; and How to solve them?](http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Funk Forty Niner Apr 24 '16 at 15:26
  • @Fred-ii- Thanks for that notice, but the error is still happening. – Programmer Dude Apr 24 '16 at 15:28
  • well someone posted an answer about it and I for one don't know what you're trying to do here, sorry. Show us the real error. http://php.net/manual/en/function.error-reporting.php and you didn't include any code for `fwrite()`. *"to create an email activation link"* - unclear. – Funk Forty Niner Apr 24 '16 at 15:28
  • @Fred-ii- I've added in more code. Hopefully this is enough to solve the problem. – Programmer Dude Apr 24 '16 at 15:32
  • thanks but you need to tell us what the exact error is. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Apr 24 '16 at 15:37
  • @Fred-ii- First off, thank you very much for the `UPDATE` notice, I forgot to put the `WHERE` clause. I've put in the error reporting function, and nothing showed up on the page. The page is still completely blank. – Programmer Dude Apr 24 '16 at 15:40
  • I don't know if it's just cause you copied it into the wrong place in your post, but your php closing bracket is cutting off some of your php code. – DaiBu Apr 24 '16 at 15:44
  • You're welcome. Well, all I can think of would be to first declare `$cookie = sha1($_COOKIE['TemporaryCookie']);` outside `$content` while making sure that cookie has already been created and exists, then try doing `$cookie=$cookie;` inside `$content`. TBH I'm a bit at a loss here. Another thing you can try is a heredoc or nowdoc http://php.net/manual/en/language.types.string.php and do a `var_dump();` on `$content` to see what reveals. – Funk Forty Niner Apr 24 '16 at 15:44
  • @Fred-ii- Thank you very much! Your answer almost resolved my solution, and I figured out that I had to escape the variable with a backslash! Sadly, I can't accept your answer as a solution, since your answer is a comment. – Programmer Dude Apr 24 '16 at 15:54
  • @ConnectionCoder You're welcome and TBH, the thought of escaping did originally come to mind and I should have followed my instinct about it. Glad it worked for you, *cheers* – Funk Forty Niner Apr 24 '16 at 15:56
  • @ConnectionCoder I transcribed it into an answer if you wish to accept it and mark the question as solved, *cheers* – Funk Forty Niner Apr 24 '16 at 18:51
  • @Fred-ii- I've marked your answer as the solution. Again, thanks! – Programmer Dude Apr 25 '16 at 03:13
  • @ConnectionCoder You're most welcome, glad everything worked out, *cheers* – Funk Forty Niner Apr 25 '16 at 12:02

2 Answers2

2

I don't know if this is the whole problem, but...

$cookie = sha1($_COOKIE['TemporaryCookie'];

...is missing a right bracket. It should be...

$cookie = sha1($_COOKIE['TemporaryCookie']);

DaiBu
  • 529
  • 3
  • 17
1

Converting my comment to an answer.

First declare $cookie = sha1($_COOKIE['TemporaryCookie']); outside $content while making sure that the cookie has already been created and exists, then doing $cookie=$cookie; inside $content.

Another thing you can try is a heredoc or nowdoc http://php.net/manual/en/language.types.string.php

As you mentioned in comments and something I did think of but didn't mention in comments was to escape the variable with a blackslash.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141