Advance apologies for this noob question but i have to do it perfectly. I have made a website which are all html files and all are working fine. Now for the session handing i have to convert all the html files into php like
<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/bootstrap-min.css"/>
</head>
<body>
Body elements
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
to something like
<?php
"SESSION HANDLING PHP CODING"
echo '<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/bootstrap-min.css"/>
</head>
<body>
Body elements
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>'
?>
Hoping for best possible solution.
Thanks
You don't need to do an echo, just close the php tag with "?>":
<?php
//CODE
?>
Your Html Stuff
You really don't have to do that - you can mix HTML and PHP easily. Just surround the code with <?php and ?> tags:
<?php /* handle session here */ ?>
<!DOCTYPE HTML>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/bootstrap-min.css"/>
</head>
<body>
Body elements
<?php echo $something_you_handled_earlier; ?>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="js/bootstrap.min.js"></script>
</body>
</html>
You actually need to refer session handling in PHP.
And then separate the logical php part from the HTML part as mentioned in other answers.
Related
I have html page like this:
<head>
<link rel="stylesheet" href="assets/css/global.css">
</head>
<body>
<script src="assets/js/script.js?1617644081"></script>
</body>
How can I replace it to be like this:
<head>
<link rel="stylesheet" href="https://example.com/assets/css/global.css">
</head>
<body>
<script src="https://example.com/assets/js/script.js?1617644081"></script>
</body>
Numbers after assets/js/script.js? change every refresh.
Simply, Using heredoc and str_replace :
<?php
$html = <<<HTML
<head>
<link rel="stylesheet" href="assets/css/global.css">
</head>
<body>
<script src="assets/js/script.js?1617644081"></script>
</body>
HTML;
$html = str_replace('assets/', 'https://example.com/assets/', $html);
echo $html;
/* Output:
<head>
<link rel="stylesheet" href="https://example.com/assets/css/global.css">
</head>
<body>
<script src="https://example.com/assets/js/script.js?1617644081"></script>
</body>
*/
?>
First I had this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<title>PHP file</title>
</head>
<body>
<h1>
<?php
echo "Hi again...";
?>
</h1>
</body>
</html>
I accessed the file through localhost/learningphp/myfirstfile.php and it rendered properly, showing me a h1 element with the text "Hi again...".
Then I changed to this:
<!DOCTYPE html>
<html>
<head>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<title>PHP file</title>
</head>
<body>
<p>
<?php
echo "Hi again...";
$myName = "Sahand";
echo $myName;
?>
</p>
</body>
</html>
Notice the change of <h1> tags to <p> tags, and the addition of myName. Still, when I go to localhost/learningphp/myfirstfile.php myName (Sahand) is not added to the page, and "Hi again..." is still shown in "h1 styling", like when I viewed the first version of the php file. Why is this and what can I do about it?
You will have two issues in this case may be
You are saving the file somewhere else or you didn't save the
updated content.
Your Browser history try a hard refresh by using Ctrl+F5 (for windows) Keys
together
I have trouble with the contruction of a working well formmated DOM, through php.
The source-code is diplayed right, but all the dev-tools of Chrome, Firefox and Edge, display the head-tag inside the body-tag. Can you please help me to spot the mistake, beacuse the frontend is now faulty displayed.
it look like this:
php-snippet:
<?php
session_start();
//doctype
echo "<!DOCTYPE HTML>\n";
//html
echo "<html>\n";
//html-head
echo "<head>\n";
include "inc/head.html";
echo "</head>\n";
//html- body start-end
echo "<body>\n
some content
</body>\n</html>\n";
?>
head.html:
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'>
sourcecode html:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'></head>
<body>
some content
</body>
</html>
You can either use file_get_contents() for this
$content = file_get_contents('head.php');
print $content;
Or use the include function but receive its output.
$content = include('head.php');
print $content;
NOTICE
Keep in mind, that if you decide to use include for this, it will execute the code inside head.php first, which file_get_contents() wouldnt.
Maybe this also helps you.
I would suggest using PHP's output buffer, changing your code to look like this:
<?php
session_start();
ob_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<?php include "inc/head.html"; ?>
</head>
<body>
some content
</body>
</html>
<?php
echo ob_get_clean();
?>
I'm learning PHP / HTML / CSS. Today I want use Bootsrap.
I use a basic HTML5 template called index.php In this file I want include a footer like this:
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="ressources/bootstrap/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="ressources/css/style.css">
<script type='text/javascript' src="ressources/script/jquery.js"></script>
<script type='text/javascript' src="ressources/bootstrap/js/bootstrap.js"></script>
<title>Index</title>
</head>
<body>
<div id="wrap">
<p>test</p>
</div>
<?php include('includes/footer.php'); ?>
</body>
</html>
But I dont see my footer in my page. I just put an "Hello World" in my footer.php
Ok, let me explain:
I have a some files, something basic like this:
index.php
<html>
<head>
<title>Simple page</title>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<div class="thisWillBeBlue">Still not blue</div>
style.css
.thisWillBeBlue {background: blue}
Now the question: Using php I want to insert the style.css inside the head tag, calling it from the file home.php. Well, I came out with a solution, but it was not very effective:
index.php
<?php $css = array();
$css[] = 'linktothecss.css'
?>
<html>
<head>
<title>Simple page</title>
<?php
foreach($css as $item){
echo "<link rel='stylesheet' href='".$item."' />";
}
?>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
But the problem it is, If I call the css from home.php it will be added to the array later, therefore it will not be echoed inside the head tag. Any ideas?
You could do it using ob_start() and ob_end_flush() functions
e.g.
index.php
<?php
$csspage = "default.css";
function loadCSS($buffer) {
global $csspage;
return (str_replace('{{ css }}', $csspage, $buffer));
}
ob_start("loadCSS"); ?>
<html>
<head>
<!-- the string {{ css }} is just a placeholder that will be replaced
with the new value of $csspage defined later in the code, otherwise
it will replaced with its initial value (default.css)
-->
<link href="{{ css }}" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
<?php ob_end_flush(); ?>
home.php
<?php $csspage = "custom_style.css"; ?>
<div class="thisWillBeBlue">blue</div>
Further reference: http://it1.php.net/ob_start
I think you are looking for something like this ..(include a piece of code in their header files, so that it will allow you to add more stylesheets )
This will allow you to add more stylesheets to it on each page.
(add this to <head>)
<?php
if (!empty($styles) && is_array($styles)) {
foreach ($styles AS $style) {
echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
}
}
?>
You can put a variable at the top of an individual script if you need a specific stylesheet:
<?php
$styles = array('custom_style.css');
?>
CSS file references can be placed in the body of your code, if needed.
<body>
<link href="linktothecss.css" rel="stylesheet" type="text/css" />
<div class="thisWillBeBlue">
I'll be blue as soon as linktothecss.css finishes loading!
</div>
</body>
The only difference is that when in the HEAD, they are guaranteed to be loaded before the page is rendered. When they are in the BODY, there may be a split-second where they are still loading and the styles haven't been applied yet.
If you definitely want them in the HEAD, you could define the css requirements in a separate folder with the same file name, like so:
index.php:
<html>
<head>
<?php
include('css-requirements/home.php');
?>
</head>
<body>
<?php include('home.php'); ?>
</body>
</html>
and
css-requirements/home.php:
<link href="mycss.css" rel="stylesheet" type="text/css" />
<link href="myothercss.css" rel="stylesheet" type="text/css" />