Php wont execute - php

When I create file index.php in directory /public/ my source code
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
<?php echo '123' ?>
</NOINDEX></NOFOLLOW>
</head>
<body>
</body>
</html>
from browser looks like
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
123
</NOINDEX></NOFOLLOW>
</head>
<body>
</body>
</html>
but if I create folder (no matter where) and place file index.php into it when source code become
<html>
<meta name="ROBOTS" content="NOARCHIVE">
<head><NOFOLLOW><noindex>
if I make some correction in this file, source code stay the same, not changed in browser view.
Why I cant create and run php code where I want, except /public/ folder?

Is that the exact code you are using in both index.php files?
If so, they should both fail as there is a syntax error. You are missing the ; at the end of the php code.
You posted: <?php echo '123' ?>
Should be : <?php echo '123'; ?>
If that´s not the case, you could add the following to the top of index.php to make sure the errors are showing on the page:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
If any error appear after that, please post them back here.

Related

simple php code not been executed

I am trying to create a simple php page which contain only an echo, if I saved the page as ".html" it will be opened on google chrome but nothing is executed, if ".php" the page never open. any help? PS: I am new on programing
<html>
<head>
<title> test</title>
</head>
<body>
echo "hello World";
</body>
</html>
You have to write your PHP code between PHP open <?php and close ?> tags.
Try this :
<html>
<head>
<title> test</title>
</head>
<body>
<?php echo "hello World"; ?>
</body>
</html>
Php file don't run .html format and without server . Please read and understand what is php and how it is work ?. follow below link or find youtube tutorial for more visual understanding .
https://www.w3schools.com/php/default.asp
http://php.net/manual/en/
Maybe you try this :
<html>
<head>
<title> test</title>
</head>
<body>
<?php echo "hello World"; ?>
</body>
</html>
Save file as .php format and run through a localhost server that you prefer . if you don't know what is server is
follow link :
https://www.w3schools.com/php/php_install.asp
PHP server on local machine?
Download XAMPP, change your code to
<html>
<head>
<title> test</title>
</head>
<body>
<?php echo "hello World"; ?>
</body>
</html>
Locate the folder C:/xampp/htdocs, and create the file index.php containing your code, now type "localhost" in your web browser and you're done

Php include add title tag

Okay people, its something very common the only thing is that I have no idea how to deal with it. i have a file "login.php" the codes within the file are
<div><textarea>Eneter your description</textarea></div>
I have a second file name "index.php" and the html inside it are
<html>
<head>
<title>SMO</title>
</head>
<body>
<?php include_once("login.php"); ?>
</body>
</html>
The problem i am facing is that, when php include login.php it also add a <title></title> tag hence i am left with two title tags.
Please help me sort it out. I know its a kid things. but just could not figure out how to solve this.
login.php
<html>
<head>
<title>SMO</title>
</head>
<body>
<div><textarea>Eneter your description</textarea></div>
</body>
</html>
index.php
<html>
<head>
<title>SMO</title>
</head>
<body>
<?php include_once("login.php"); ?>
</body>
</html>
Is this your code?
If yes, you don't have to add the <title> in your login.php page.Remove <title> and it'll work fine.
If no, please include your code in your question.
So your login.php page should be like
<div><textarea>Eneter your description</textarea></div>

PHP include file.php having CSS issues

I have a index PHP page where I include all PHP files like index.php?page=example. All pages are in another folder, here is the structure:
public_html/index.php
public_html/css/style.php
public_html/pages/
Index calls the CSS file from css/style.php.
Pages are called from index.php like (include pages/example.php) using GET function.
If I run index.php I get no problems with CSS, if I run only the included page like example.php I get CSS problems because the CSS is in index.php and obviously will not show the CSS correct.
But when I run the index.php and include the index.php?page=example then the index CSS show correct but the classes from the included pages does not work...
I suppose the include will only import the code but it seems like something is wrong with the server or I am doing something wrong?
Here is a example code of what I am using. This is index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php
include('pages/example.php');
?>
</body>
</html>
Index.php all css classes works fine but the style class from the included pages does not work they are just not styled
You shouldn't write your css code in a php file. Better create a css file and put your style directives in there. You can include css styles best by following conventions, create a basic html template like the following and link to your css file and include the php in there.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="part/to/file.css"> <!-- link your stylesheet here -->
</head>
<body>
<?php
include('path/to/file.php'); // include your php code here
?>
</body>
</html>
Make sure you have header("Content-type: text/css"); as your first line in php file so it renders correctly as css. Then do not include the file. Instead refrence to it like a normal css file only change the .css to .php. <link rel="stylesheet" href="part/to/file.php">. That should get you working. I am assuming your pulling data from a database to fill in your css, so make sure it is format correctly. Do not use something like .headertext{
color:<?=$row['headercolor'];?>; . Instead declare it in php tags. $color= $row['headercolor']; . Then in css part of php file call that variable. .headertext{
color:<?=$headercolor?>;. Hope that helps

php web page not loading

I am not an expert but I am no noob at PHP, yet for whatever reason I am stomped as to why my document will not load. Here is my code.
<?php include 'header.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<p>Hello everyone</p>
</body>
</html>
When I pull out the PHP portion the HTML loads fine. Here is the code in my header.php file.
<?php
<href="index.php">Home</a>
?>
I have tried this on two different hosts, both of which are hosting other PHP websites and still getting issues. I have also validated it with W3Schools and another online PHP validator. Both didn't find any errors. Any help would be greatly appreciated.
Enable errors to see errors, this way:
ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);
This code is a PHP error:
<?php
<href="index.php">Home</a>
?>
Try change to:
<?php
echo '<href="index.php">Home</a>';
?>
This:
<?php
<href="index.php">Home</a>
?>
Is no valid PHP. This would, however work:
Home
Inside of the PHP-tags you can only use PHP - no HTML. Also, <href> is no HTML tag.
Look at this question How to get useful error messages in PHP? to find out, how to enable error messages in PHP.

Using better includes for pages?

As of right now, the way I use includes is to bring the header, footer, and some content for other pages.
This leads to more includes then I really want, because I need to add more content for the includes.
For example:
<!DOCTYPE html>
<?php include('header.php'); ?>
<body>
<?php include('body-top.php');
custom html
</?php include('footer.php');
</body>
It would be nice if I could add variables to the includes and on the pages I want the includes to show.
I am not good at PHP at all, so is there a better way to use Includes?
This can be easily done:
index.php
$title = 'Hello World!';
include 'content.php';
content.php
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body></body>
</html>
The problem with this approach is, you'll soon run into problems keeping track what went where, so using functions as suggested in other answers might be a good idea. However, for small projects it's IMHO good enough.
sounds like a job for Smarty
It looks like this
<?php
require 'Smarty/libs/Smarty.class.php';
$smarty = new Smarty;
$smarty->assign('title','Hello World');
$smarty->assign('hello','Hello World, this is my first Smarty!');
$smarty->display('test.tpl');
?>
test.tpl
<html>
<head>
<title>{$title}</title>
</head>
<body>
{$hello}
</body>
</html>
Or even better way, use some of the PHP MVC frameworks, which will give you even more stuff (not just template system)
Your includes are already very few, no need to optimize them.
Also don't pay attention to people suggesting Smarty or MVC's because that will increase dramatically the number of includes (in exchange for other benefits, of course)-
You can turn your included files into functions. PHP has a neat trick where anything between curly-brackets (i.e. { and }) is only executed when that part of the code is reached. This includes the HTML code outside of your PHP tags.
This could be our 'header.php' file, where we wrap our current code in a function.
<?php function doHeader($title) { ?>
<html>
<head>
<title><?php echo $title; ?></title>
</head>
<?php } ?>
Then we make a tester for it. Whatever our tester/caller chooses to pass as $title shows up in our output.
<?php
// All included here
include_once('header.php');
?><!DOCTYPE html>
<?php doHeader('My page title'); ?>
<body></body>
</html>
This produces the output,
<!DOCTYPE html>
<html>
<head>
<title>My page title</title>
</head>
<body></body>
</html>

Categories