PHP Require is not working as I expected - php

Please review the below code and advise what is wrong with it when using require.
First here is the code that works (not using require in it):
<?php
//require("_start.inc");
session_start();
if(!isset($_SESSION['user']))
{
echo "<h1 style='color:red'>Please <a href='../index.php'>login</a> first!</h1>";
}
else
{
?>
<!DOCTYPE html>
<html>
<head>
<title>Main window</title>
</head>
<body>
<h1>Admin panel</h1>
<?php
require("header.inc");
?>
</body>
</html>
<?php
//require("_end.inc");
}
?>
And here is the version that doesn't work, _start.inc and _end.inc are exactly what is written in the above code, but the below one give me an error:
Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\admin_start.inc on line 9
<?php
require("_start.inc");
/*session_start();
if(!isset($_SESSION['user']))
{
echo "<h1 style='color:red'>Please <a href='../index.php'>login</a> first!</h1>";
}
else
{
*/
?>
<!DOCTYPE html>
<html>
<head>
<title>Main window</title>
</head>
<body>
<h1>Admin panel</h1>
<?php
require("header.inc");
?>
</body>
</html>
<?php
require("_end.inc");
//}
?>

You cannot have an opening brace in one and the corresponding closing brace in the other file. Includes doesn't work like copy&paste. The files always must still be valid.
Btw: Use an IDE. It will tell you about such errors.

Related

Select php included html element with jQuery

I'd like to select an included html element with jQuery. How can I achive this?
index.php
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
file.php
<?php
echo '<div class="test">test</div>';
?>
select.js
let a = $('.test');
console.log(a); // returns undefined
Try executing your script after the page has fully loaded.
$(window).on("load", function() {
let test = $('.test');
console.log(test);
});
Code should be organized as follows: <?php Php.Code.Goes.Here ?>. This can be surrounded by HTML, for instance, <b><?php print("HELLO!"); ?></b>. This will display: HELLO!
So...you see the problem then, with...
<?php
<html>
<head>
...
</head>
<body>
include('file.php');
</body>
<html>
?>
The HTML is IN the PHP. You want this:
<html>
<head>
...
</head>
<body>
<?php include('file.php'); ?>
</body>
<html>
This code, of course, shouldn't have executed at all, since <?php <html> isn't really valid PHP syntax.

Require doesn't work in basic html / php code

So im trying to import a header.php into my index.php file file but it just doesnt work.
index.php:
<?php
require "header.php";
?>
<main>
<p> online! </p>
<p> offline! </p
</main>
<?php require "footer.php"; ?>
header.php:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<p> this should be on top</p>
</header>
</body>
</html>
Footer:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p> This should be on the bottom </p>
</body>
</html>
when i open the project i will only see what is written in index without header or footer
Your code return invalid HTML, because DOCTPYE, html, head and body are defined twice.
Change header.php to:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<header>
<p> this should be on top</p>
</header>
#-- not closing body and html
Change footer.php to:
<p> This should be on the bottom </p>
</body>
</html>
Make sure the path to your header and footer is correct.
Then try including it in your page like this
require(“header.php”);
//Your html code here
require(“footer.php”);

passing php variables from dynamically created HTML Document

How to pass the php variables from dynamically created HTML page to next php file.
For Example. I have the following php code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>
Now when I see dynamically created HTML page, it just shows me the following code with .html extension; so how can I pass the php variable from this page to next file:
<html>
<head>
</head>
<body>
<a href='comments.php?id=`$uid`'> comments</a>
</body>
</html>
try to replace
<a href='comments.php?id=`$uid`'>
to
<a href='comments.php?id=$uid'>
try this... Your mistake is here 'comments.php?id=$uid' is string no php code use correct like this 'comments.php?id=".$uid."'.
<?php
session_start();
$uid = $_SESSION['uid'];
$doc = new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
<a href='comments.php?id=".$uid."'> comments</a>
</body>
</html>
");
echo 'wrote:' . $doc->savedHTMLFile("/home/user/project1/test1.html") . 'bytes';
?>
Basically, you can use $_GET[''] method to do it. You just insert it into the URL, but I think, this will make the site vulnerable to SQL Injection.
remove back ticks(`) around $uid or use this code
<?php
session_start();
$uid=$_SESSION['uid'];
$doc=new DOMDocument('1.0');
$doc->loadHTML("
<html>
<head>
</head>
<body>
comments
</body>
</html>
");
echo 'wrote:'. $doc->savedHTMLFile("/home/user/project1/test1.html"). 'bytes';
?>

HTTP redirect with header function does not work

header("Location: …" ); does not seem to be working:
<?php
session_start();
if (isset($_SESSION['user'])){
?>
<html>
<head>
<title>
Admin Panel
</title>
</head>
<body>
</body
</html>
<?php
} else {
header("Location: http://echo2.site40.net/cms/admin/login.php" );
}
?>
Seems to be working if I exactly copy paste the code...
Still look for the empty white-spaces before start of the <?php

"HTTP_USER_AGENT" not working. nothing is displayed in my browser i.e. firefox

<?php
$agent=getenv("HTTP_USER_AGENT");
if(preg_match("/MSIE/i", "$agent")){
$result="You are using Microsoft Internet Explorer.";}
else if (preg_match("/Mozilla/i", "$agent")){
$result= "You are using Firefox.";}
else {$result = " you are using $agent.";}
?>
<html>
<head>
<title>Browse Match Results</title>
</head>
<body>
<?php "<p>$result</p>";?>
</body>
</html>
Might I venture a guess, and suggest that:
<?php "<p>$result</p>";?>
Should be:
<?php echo "<p>$result</p>";?>
<?php echo "<p>$result</p>";?>

Categories