PHP Response without HTML - php

I'm trying to get the response from database for further usage.eg: userID
However, instead of just getting the userID as the answer,
I'm getting the response from php server with HTML
It shows every HTML tag that I had in my php file,including the comments
EDIT
The php file just simply look like this
<?php
include "Header.php";
include "main.php";
$path = $_SESSION['username'].$_POST['Case_no'];
mkdir($path);
echo $path;
?>
Everything working fine,but the output look something like (let say the $path is "1234")
1234 口 <!--HTML comments and scripts from the webhost-->
If there is any other HTML code in the php,say a link at the front
The result shows
Link 1234 口 <!--HTML comments and scripts-->
Anyway to get rid of this?
The required answer is just merely 1234
ADD ON
I have tried the following example in http://www.hassanpur.com/blog/2010/09/android-development-implementing-a-simple-client-server-model/ (where you can see the whole code)
Instead of getting the "Chirp chirp" as response,
I'm getting sth like "Chirp Chirp 口 " –

Like Mat said, PHP outputs whatever you tell it to output. Don't tell it to output HTML if you don't want that.
if you don't want to output some HTML, persumably written in Header.php and main.php - DO NOT include these files

The problem is solved by using my PC as the server by using the same coding I have shown. The HTML tag and comments is actually generate by the free webhosting side

send "text/plain" header to tell the client that this text shoudn't be interpreted as HTML, because default content type is HTML
header("Content-Type: text/plain");

Related

ob_get_contents prints php tags instead of executing the PHP code

so I have this code:
$template_file = 'template.tpl.php';
ob_start(); // Start output buffering
include "./$template_file"; // Include the template file
$contents = ob_get_contents(); // Get the contents of the buffer
ob_end_clean(); // End buffering and discard
return $contents;
this is what template.tpl.php looks like:
<?=translate_string('A message to display to user')?>
however when I inspect the content of $contents, instead of having it display 'A message to display to user' it instead displays <?=translate_string('A message to display to user')?> ... ie. it displays the PHP code in entirety instead of executing the PHP code and simply return the output of the executed code...
Any idea on what could possibly cause this?
I'm using Drupal 6
EDIT
looks like short_open_tag setting is On...any other possibilities?
also it would be great if I can still use <?= notation without making it <?php etc... since it's used quite prevalently
Further Update
looks like jszobody no longer have further contribution, if anyone else knows what could possibly cause this other than the short_open_tag setting, feel free to answer
thx to jszobody for the short_open_tag contribution
You are using PHP short tags in your template code. If those are not enabled on your server, it's not parsed by PHP, and instead treated as plain text.
See here: http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
I'd change your template to this:
<?php echo translate_string('A message to display to user'); ?>

Does echoing javascript always work in php?

In php, if I put the following line
echo "<script type='text/javascript'>[javascript code here]</script>";
can I assume that the javascript code will be always be executed irrespective of where I put it in a php file? Assume that the php syntax is valid and the line gets executed (it is not barred by some conditions)
Edit: I have a php file where I have some javascript code and alert('ok'); but the messagebox never appears. I was wondering if the code actually got executed and the browser dismissed the messagebox as the page was changed.
No.
You might put it in a PHP file that doesn't output HTML (e.g. a PDF document or a zip file).
You might put it above a header() call and break the header
You might put it inside an if statement body so it would only be included conditionally
You might put it inside a <style> element, so it would be treated as invalid CSS
etc
PHP just outputs stuff. If you want it to output a <script> element, then you have to put it somewhere where it will be output and it somewhere where outputting it makes sense.
Yes it will be executed because it is returned to browser as html code and browser does not care where it comes from. It is also often abused in XSS attacks.
To conclude it will execute always until it is correct JS and it prints in proper place in html structure.
The code will be executed if the place in which it is inserted is valid. It will behave identically to if you had simply written the javascript code there.
For example, I'm sure the following code wouldn't work:
<ta<?php echo "<script type='text/javascript'>[javascript code here]</script>"; ?>ble>
You are messing with two pairs of shoes here. Your javascript code will not be executed anywhere on the server side (where PHP is executed). After the PHP prozessor is done and the output (including your line of javascript) is send to the client your javascript will be executed. Depending on how strict the clients browser is your <script>-element needs to be inside of the <head> or <body> Element of your HTML-page

file_get_contents not actually grabbing file? Blank?

Try this sample code I threw together to illustrate a point:
<?php
$url = "http://www.amazon.com/gp/offer-listing/B003WSNV4E/";
$html = file_get_contents($url);
echo($html);
?>
The amazon homepage works fine using this method (it is echoed in the browser), but this page just doesn't output anything. Is there a reason for this, and how can I fix it?
I think your problem is that you're misunderstanding your own code.
You made this comment on the question (emphasis mine):
I've never used those utilities before, so maybe I'm doing it wrong but it only seems to be downloading this page: https://www.amazon.com/gp/offer-listing/B003WSNV4E/ref=dp_olp_new?ie=UTF8&condition=new
This implies to me that an Amazon page is appearing in your browser when you run this code. This is entirely expected.
When you try to download https://rads.stackoverflow.com/amzn/click/B003WSNV4E, you're being redirected to https://www.amazon.com/gp/offer-listing/B003WSNV4E/ref=dp_olp_new?ie=UTF8&condition=new which is the intent of StackOverflow's RADS system.
What happens from there is your code is loading the raw HTML into your $html variable and dumping it straight to the browser. Because you're passing raw HTML to the browser, the browser is interpreting it as such, and it tries (and succeeds) in rendering the page.
If you just want to see the code, but not render it, then you need to convert it into html entities first:
echo htmlentities($html);

PHP files not loading properly

I have a PHP file consisting of the following structure:
<html>... headers, scripts & styling
... some html here
<?php
if($_GET['v'] == 1)
{
?>
... html code here ...
<?php
}
else
{
?>
... html code here ...
<?php
}
?>
</html>
Sometimes the file just loads half, for example if v=1 what would load onto the screen (if I check with View Source also) is something like this: (relative to what I exampled above)
<html>... headers, scripts & styling
... some html here
... html cod
As you can see, the code just cuts off randomly. The is nothing obvious casing this such as a loop or anything. It happens in the middle of HTML code and not inside the <?php ?> tags.
It looks as if the server just decides to stop communicating right there-and-then for no reason. It also happens at a different and random place each time and sometimes loads perfectly fine.
It also only happens on my shared hosting account and not on my localhost.
Is there anything simples that might be causing this?
Did anyone experience this before?
Your code produces a warning (apparently silent) and fails here:
if($_GET['v'] == 1)
if no v parameter was given in the query string.
Do it like this:
if(isset($_GET['v']) && $_GET['v'] == 1)
If you're running an old version of PHP you'll have to make two separate if statements for each of the two conditions.
Make sure you have display_errors turned on.
ini_set('display_errors',1);
Just to make sure there's nothing going horribly wrong.

Self modifying PHP script

I'm trying to modify a part of a PHP script structured like this barebone example
<-- part A -->
function modify_B($string)
{
some code to modify part B
}
<-- end A -->
<-- part B -->
<container>some XML</container>
<-- end B -->
<-- part C -->
<-- end C -->
I'd like to modify part B without changing the rest of the file, because A and B are the logic of the script which should not change.
Could somebody help me?
Thank you in advance for your help.
It looks like, from your example, it's just some string data XML. So load the content into a string somehow (either set a variable with standard string notation, or read it from the contents of a separate file), modify the string according to your whims, and then echo the string to the output. Then it's not a problem of being self-modifying anymore. It's just a matter of being data-driven.
Do you mean that the XML is outside of your PHP <? ?> script tags? So you want to modify the text that's about to be output by the PHP script?
If that's the case, remember that anything outside of script tags is just treated as a string, which PHP outputs as if you had written echo $string;. So just save your changed data in a string variable, and echo it.
Or if you need persistence in the changes, put "B" in a file and include or read it.
You should never write self modifying code (unless you are writing assembly) it can cause all sorts of problems, consider for example what happens if there is a bug that destroys the code in the file.
Split your data out from the code and load it with a require_once command
You can then use standard file reading and writing commands to edit the data http://www.php.net/manual/en/book.filesystem.php
... or better still since the data is XML, save the file as an XML file and use simple xml to maintain the data http://php.net/manual/en/book.simplexml.php

Categories