Whether I enter the value for bug id or not ..in both conditions the code between php tags is displayed as output. Can someone help me to find out the reason.
Code is given below:
html file-------------------------------------------------------------
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bug Report</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h2>Bug Report</h2>
<form action="test.php" method="post" >
<p>Bug ID:<input type="text" name="bugid" size="20" /></p>
<p><input type="submit" value="Record Bug" /></p>
</form>
</body>
</html>
php file--------------------------------------------------
<!DOCTYPE html PUBLIC"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Record Bug</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
</head>
<body>
<?php
$bugid=$_POST["bugid"];
echo $bugid;
if (empty($bugid))
{
echo "<p>You must enter the Bug ID to record the bug.</p>";
}
else
{
echo"<p>good</p>";
}
?>
</body>
</html>
If you're getting PHP code in the output, then your webserver isn't running that page/script through the PHP interpreter. Generally, that's because you've put the code into a .html file, which is not treated as PHP by default.
Either rename the file to whatever.php, or reconfigure your webserver to treat .html files as PHP scripts.
check that php is working or not for that write the code <?php phpinfo(); ?> and if have manually installed php apache and getting problem try wamp server
your code is widely open for sql-injunction to make it secure use
public function mysql_prep( $value ) {
$magic_quotes_active = get_magic_quotes_gpc();
$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
if( $new_enough_php ) { // PHP v4.3.0 or higher
// undo any magic quote effects so mysql_real_escape_string can do the work
if( $magic_quotes_active ) { $value = stripslashes( $value ); }
$value = mysql_real_escape_string( $value );
} else { // before PHP v4.3.0
// if magic quotes aren't already on then add slashes manually
if( !$magic_quotes_active ) { $value = addslashes( $value ); }
// if magic quotes are active, then the slashes already exist
}
return $value;
}
Check whether php is running or not in your machine. Save the below code as test.php and run it through
<?php
phpinfo();
?>
In that case you have to run on that Server which support PHP like Xampp or Wamp and also extension of the file should be .php
Related
I am trying to create a template html page which I will call via an include to set to a variable, this variable will then be used to set the value of a new file. I need the variables in the included file to be resolved so that the values are populated correctly.
To demo imagine these files:
main.php
$someVar = "someValue";
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
if (file_put_contents($newFileName, $fileText) !== false) {
echo "File created (" . basename($newFileName) . ")";
} else {
echo "not created";
}
aTemplate.php
<?php
return
'<!doctype html>
<html lang="en">
<head>
<title><?php echo $someVar; ?></title>
</head>
</html>'
?>
What is currently happening is that the variables stay unresolved and hold no value so in the created html file the title is:
<title></title>
Instead of
<title>someValue</title>
How can I change the 'aTemplate.php' file to resolve the properties set in 'main.php'?
Just use this at your aTemplate.php:
<?php
return '<!doctype html>
<html lang="en">
<head>
<title>'. $someVar .'</title>
</head>
</html>';
?>
There are a couple of problems with your template, firstly as you have the HTML in single quotes, this won't do any of the string substitutions. Secondly, your trying to do a PHP echo whilst in HTML in PHP. I've used Heredoc to enclose the HTML as it allows any sorts of quotes and will also do the replacements.
The substitution of the value is just replaced by adding $someVar directly into the string.
So aTemplate.php becomes...
<?php
return <<< HTML
<!doctype html>
<html lang="en">
<head>
<title>$someVar</title>
</head>
</html>
HTML;
You should echo those string in page instead of return command.
The keyword return is used inside a function while your file is not a function. The browser simply puts what's inside include file has to offer. In you case it is HTML string which should be outputted using echo command.
Also the server executes code in top to bottom and left to right. Thus the variable $someVar will be accessed in aTemplate.php file.
Use below code instead to work
main.php
$someVar = "someValue";
$file = 'aTemplate.php';
// Open the file to get existing content
$fileText = include "aTemplate.php";
$newFileName = 'someFile.php';
// Write the contents back to the new file
if (file_put_contents($newFileName, $fileText) !== false)
{
echo "File created (" . basename($newFileName) . ")"; }
else {
echo "not created";
}
aTemplate.php
<!doctype html> <html lang="en"><head>
<title><?php echo $someVar;?></title>
</head>
</html>
I have issue to redirect another location after fopen() function in php. below is my function which i m using.
<?php
function create_file($filename){
$my_file = 'folder/index.php';
$fh = fopen($my_file, "wb");
$data = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Some data</title>
</head>
<body>Here some data</body>
</html>';
fwrite($fh, $data);
fclose($fh);
return true;
ob_end_clean();
exit();
}
$file = create_file(test);
if($file == true){
$url = 'http://example.com';
return $url;
}
else{
return 0;
}
?>
If you want to redirect in PHP, you cannot send any output to the server before header("Location: http://example.com");.
This includes any HTML, text or white spaces that aren't wrapped in a PHP tag set.
The header function must be called before any page output (if HTML has already been displayed, it's too late for your headers!).
I'm trying to create a page what will log a plethora of user data for security purposes but am am unsure how best to do so.
Ideally I want all important stats for example their IP, their X-Forward IP, their browser, operating system and any other details I can get.
I've also got problems showing the variables in a php / html page as shown below:
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
$template = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title</title>
</head>
<body>
IP: <?= $ip ?>
IP: [IP]
</body>
</html>';
$template = str_replace('[IP]', $ip, $template);
echo $template;
?>
or
<?php
$ip = <?= $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
Try something simple first:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
echo $ip;
?>
For a full list of SERVER superglobal variables, check this link http://php.net/manual/en/reserved.variables.server.php
<?php
$data = $_SERVER;
$IP = $data['REMOTE_ADDR'];
echo "IP address is:" . $IP;
?>
There you go... now look up all the available $_SERVER values and use the ones you need.
Download link:
Download php file
download.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Home</title>
</head>
<body>
<h1>PHP file</h1>
<?php
function get_text($text)
{
....
}
function get_time($time)
{
....
}
$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];
$xml = simplexml_load_file($url);
if (count($xml))
{
foreach($xml->book as $book)
{
echo ....
}
}
?>
</body>
</html>
The download.php is a ready made API php script to provide webmasters upload to their FTP. Webmasters can be choose many options(e.g: download.php?words=2000&sort=popular&type=xml) from a form, then submit the form to get their custom API script.
This is the line that will replace the options after they submit the form.:
$url = "http://api.xxx.com/info.php?words=".$_GET['words']."&sort=".$_GET['sort']."&type="$_GET['type'];
This is the code to force download. But i don't know how to wrap whole page with $content = "";. I know how to wrap the HTML codes but how to wrap the PHP function and codes on the page?
header("Content-Disposition: attachment; filename="download.php");
print $content;
Not sure if that is what yo want; but you could create a second script that calls the first one, gets the output, and sends that with the mentioned headers:
<?php
$words = (int) $_GET['words'];
$sort = $_GET['sort'];
$url = sprintf("http://localhost/wherever/download.php?words=%d&sort=%s&type=xml", $words, $sort);
$content = file_get_contents($url);
header("Content-Disposition: attachment; filename="download.php");
print $content;
?>
Call that file "force_download.php" and let the users call Download php file instead.
So I downloaded and edited a script off the internet to pull an image and find out the hex values it contains and their percentages:
The script is here:
<?php
$delta = 5;
$reduce_brightness = true;
$reduce_gradients = true;
$num_results = 5;
include_once("colors.inc.php");
$ex=new GetMostCommonColors();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Colour Verification</title>
</head>
<body>
<div id="wrap">
<img src="http://www.image.come/image.png" alt="test image" />
<?php
$colors=$ex->Get_Color("http://www.image.come/image.png", $num_results, $reduce_brightness, $reduce_gradients, $delta);
$success = true;
foreach ( $colors as $hex => $count ) {
if ($hex !== 'e6af23') {$success = false; }
if ($hex == 'e6af23' && $count > 0.05) {$success = true; break;}
}
if ($success == true) { echo "This is the correct colour. Success!"; } else { echo "This is NOT the correct colour. Failure!"; }
?>
</div>
</body>
</html>
Here is a pastebin link to the file colors.inc.php
http://pastebin.com/phUe5Pad
Now the script works absolutely fine if I use an image that is on the server, eg use /image.png in the Get_Color function. However, if I try and use an image from another website including a http://www.site.com/image.png then the script no longer works and this error appears:
Warning: Invalid argument supplied for foreach() in ... on line 22
Is anyone able to see a way that I would be able to hotlink to images because this was the whole point of using the script!
You must download a file to the server and pass its full filename to the method Get_Color($img) as $img parameter.
So, you need to investigate another SO question: Download File to server from URL
The error indicates that the value returned by Get_Color is not a valid object that can be iterated on, probably not a collection. You need to know how the Get_Color works internally and what is returned when it doesn't get what it wants.
In the mean-time, you can download [with PHP] the image from the external url into your site, and into the required folder and read the image from there.
$image = "http://www.image.come/image.png";
download($image, 'folderName'); //your custom function
dnld_image_name = getNameOfImage();
$colors=$ex->Get_Color("/foldername/dnld_image_name.png");
By the way, did you confirm that the image url was correct?