multi line die() message - php

Well it's a very simple question but I haven't found the answer. Hopefully it'll not take too much of anyone's time to just show me.
I want to output a multi line die() message and I've tried variations of \r\n and PHP_EOL with quotes here and not here and things like that: and I can't get it to work.
Here's the basic line I want to edit:
if(mysql_num_rows($res) > 0) die("You Will Be Reported For Spam To Your ISP" . mysql_error());
I just want to expand that "you will be reported..." into a little para of dire threats..

That's because whitespace is folded in HTML. If you need to break the line then you will need to do it as HTML does it: with the <br> tag.

Does this help?
<?php
// line breaks will appear if running on command line
if(mysql_num_rows($res) > 0) {
die("You Will Be Reported\r\nFor Spam To Your ISP" . mysql_error());
}
// paragraphs will appear on a web page
if(mysql_num_rows($res) > 0) {
die("<p>You Will Be Reported</p><p>For Spam To Your ISP</p><p>" . mysql_error() . "</p>");
}
?>

Mate, if you want to die() preformated text (having \r\n) not in html context just use
die("<pre>" . $your_multiline_preformated_text . "</pre>");
this should do it. If you want to use HTML tags in your die() output, you need to inform the browser first (so either printout some "< html >< body >" first or feed the browser with a php header 'content-type').

Related

What factors that affect the single line comment?

I just started to learn the php and found interestingly new only to me that single line php can affect the code (break the code and may output the html):
From the docs:
// $file_contents = '<?php die(); ?>' . "\n";
Which results in ' . "\n"; (and whatever is in the lines following it) to be output to the HTML page.
But using comment on this wouldn't affect the code:
$file_contents = '<' . '?php die(); ?' . '>' . "\n";
Ah, its only because of < and > or something else?
So, I'm curious to know what exactly the factors that affect from using single line comment?
This is an excerpt from the php.net website mentioned in my comment:
The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that. If the asp_tags configuration directive is enabled, it behaves the same with // %> and # %>. However, the tag doesn't break out of PHP mode in a one-line comment.
In your example, it would be the ?> that is breaking out of the comment as new lines and end PHP tags will override comments to end the script, which is why when you split the ? and the > into two strings and concatenated them, it didn't end the comment.

Printing from PHP with correct line breaks

My code works, but I'm having trouble with line breaks...
I have data saved to a variable as such within a foreach loop:
$printerdata .= " " . $product_name . "\n";
$printerdata .= " $". $price . ".00 \n";
$printerdata .= " " . $displayoptions . "\n";
$printerdata .= "\n";
Then I post that data and use sockets to print to the printer from my php code. Like I said before, my code works great, but the problem lies with line breaks. When printed from one browser the data is displayed correctly with "\n" from the code above, however, when printed from the browser I'm being forced to use called Fresco, I get whitespace between the data rather than line breaks.
I've tried "\r\n" and that still does not produce any line breaks. I've also tried <br> but is literally printed out. Does anyone have any ideas on other ways to produce line breaks within my code? Thanks for the help!
The php function nl2br could help you. This function inserts HTML line breaks before all newlines in a string. You can look it up here:
nl2br Manual
The correct tag is
</br> not <br>
But I think in fresco there is also a tag
<af>

php - how to hide the HTML part when no session_start();

thank you in advance , i wanted to stop not logged_in visiters from reaching some pages
i know i can just do that by cheking values in SESSION_ and and showing the HTML part with
<?php echo '<html>...</html>'; ?>
but i have a very long html content with both '' & "" text delimiters because it's not only HTML there is some JavaScripts, so i'm looking for another methode rather than printing the code with ECHO , and in this case i can't mixt PHP and HTML code . i there any solution or another idea to secure page from non member to see it,
and can i use this code to take them back to hope page (? :
<?php header("Refresh: 0;url=http://index.php/"); ?>
is it secure ?
so i'm looking for another methode rather than printing the code with
ECHO ,
use [heredocs] style or print all HTML outside php tag:
1- [heredocs]
echo <<<HEREDOCS
any string here ' or "
HEREDOCS;
note: notations should be at beginning of each line and ends with line break without any space or indent.
2- separation logic
<?php
//here is php
//close php
?>
<html>
<head>
</head>
<body>
<p>This will also be ignored by PHP and displayed by the browser.</p>
....
</body>
<?php
//php again
?>
When you echo out code that uses both ' and ", you escape the characters that match your opening and closing tags, like so:
echo '<p class="someclass">This can\'t be the end.</p>';
or
echo "<p class=\"someclass\">This can't be the end.</p>";
If you're worried about the " & ' text parameter,you should just use text editor such as sublime to to replace all " to '
and for " in textarea you can replace with "
then do
echo " <a href='#' value='{$var1}'>double quote mean "</a>some other massive code ";
thus you can just use a <?php and ?> from the beginning of page till the end of the page
What you can probably do is perform the check for the session variable at the start of the file and if the user is not logged in, use a header('Location: index.php'); followed by an exit();. If you do need to send headers based on logic that appears after the echo statements and changing the order of the statements is not doable, use output buffering.
Also, instead of including the HTML and Javascript within your code file, consider using a template engine, using include, or a combination of file_get_contents and echo (and possibly a string replace if you are using content placeholders). Heredocs and Nowdocs will make it easier to include single-quotes and double-quotes within strings.
Edit: If all you want to show unauthenticated users is an error message, you can redirect them to an error page, or use the PHP die(''); statement (which is like a combination of an echo and an exit).

How do I echo slashes in php?

Everytime I check the HTML output of this piece of code, the slashes aren't included. Thus, the background image fails. I even put it through a html to php converter. Im lost; please help.
while($row = mysql_fetch_array($data))
{
//Echo Theme Template on pages
echo "<div style='background-image:url('../uploads/avi/{$row['avi']}')></div>";
echo "<div class='myname'>{$me}</div>";
}
the simplest answer would be, you have an unclosed ' on your style attribute..
echo "<div style='background-image:url('../uploads/avi/{$row['avi']}')'></div>";
^here
but this wouldn't work as is.. so you should adjust the quotes like this:
echo "<div style='background-image:url(\"../uploads/avi/{$row['avi']}\");'></div>";
you can see the broken echo http://codepad.viper-7.com/CGMdUx
and the edited one is here http://codepad.viper-7.com/bEyKFz
i passed it through htmlspecialchars on codepad just so you can see it as string and avoid being rendered as HTML for viewing purposes only..
You did two things wrong here. You never closed the final quote for the style tag, and using single quotes for both style='' and the url('') cancelled each other out.
I'd recommend always using double quotes for HTML tags.
while($row = mysql_fetch_array($data))
//Echo Theme Template on pages
{
echo "<div style=\"background-image:url('../uploads/avi/{$row['avi']}');\"></div>";
echo "<div class='myname'>{$me}</div>";
}
Another thing to consider, always say "View Source" instead of "Inspect" with Firebug or some other tool. The reason you didn't see the URL printed out is because new browsers are "Smart" and try to fix DOM errors. Inspecting the source with Firebug or a similar tool will show you what the browser actually interprets. Viewing the source will show what was actually sent to the browser.
Because of the inner ' used by the style attribute, you need to use double quotes inside.
echo '<div style="background-image:url(\'../uploads/avi/' . $row['avi'] . "')></div>";
Result
<div style="background-image:url('../uploads/avi/abc.efg')></div>

PHP: codecomments inside functions prevents it work

$query = $connect->prepare("SELECT firstname, lastname FROM users WHERE id = '$id'");
$query->execute();
$row = $query->fetch();
// $full_name = $row["firstname"] . " ".$row["lastname"];
$full_name = $row["firstname"] . " ".substr($row["lastname"], 0, 1).".";
return $full_name;
If i remove the line that is a comment ( // ), it will return $full_name, if its there then it wont work. I also tried commenting with #, but it still wont work(wont return anything) as soon as there is a codecomment
weird issue
The only thing I can think if it's really the code: check the newline character settings in your editor or try to open it in another editor. Maybe the php parser see it in one line.
But I dont think so.
It has never happend to me.. very strange.
Try to add some text after // or put everything between this another kind of comment a code
/* the code */
Your comment shouldn't have any effect on the execution.
Have you tried turning on all the error handling? What are the contents for var_dump($row); ?
Try to detect strange invisible characters. On linux I do it with "cat -e file.php".
For all the strange-syntax-errors-because-of-one-line-in-file I try it.
Another way to test it. Remove all the lines between your $row =... and $full_name =... and then redo your commented line (no paste, re-type it).
This should be basic debugging (as well as a literal sanity check), however I'll post it as an answer; First of all turn all errors on, then:
echo 'before: ' . $full_name;
// $full_name = $row["firstname"] . " ".$row["lastname"];
echo 'after' . $full_name;
I do realise this is insane! A comment is a comment and will never have any effect on your output.
Both echos should trigger a warning since there is no $full_name variable defined yet; However, if both are printed you'll know (as we all know) the problem isn't the comment itself... (At this point, my guess is that $full_name = " "; because $query->fetch(); returned false...)
Now if none of those echos executed: Are you sure you're calling the exact same function? Didn't you misspelled it or something and happen to have errors off??

Categories