I would like to include a site name from a file called sitename.php I have tried
$thisPage='include "sitename.php" '; ?>
and get error report. I also need to add the file into
include("../../../EngineerLocation/sitename.php");
instead of the words sitename I would like to include the sitename from the php file
You can either include an actual php file:
// sitename.php
<?php
$thisPage = "Page Name";
// otherpage.php
<?php
include("sitename.php");
echo $thisPage;
Or use file_get_contents
// sitename.txt
Page Name
// otherpage.php
<?php
$thisPage = file_get_contents("sitename.txt");
To include a file using the name in sitename, do the following:
<?php
$thisPage = file_get_contents("sitename.txt");
include("../../../EngineerLocation/{$thisPage}.php");
try:
include("$_SERVER[DOCUMENT_ROOT]/sitename.php");
Related
I want to change default download location in php
Using header function I cant' find a parameter for that
This is the definition of header
header(string $header, bool $replace = true, int $response_code = 0)
Edit:
URL of solution
Save current page as HTML to server
<?php
// Start the buffering //
ob_start();
?>
Your page content
<?php
echo '1';
file_put_contents('yourpage.html', ob_get_contents());
?>
Instead of yourpage.html you specifiy the path and it works
Is there any solution for header or other alternative to just change default download location
Thanks
URL of solution
Save current page as HTML to server
<?php
// Start the buffering //
ob_start();
?>
Your page content
<?php
echo '1';
file_put_contents('yourpage.html', ob_get_contents());
?>
Instead of yourpage.html you specifiy the path and it works
So I would like to print name of the current page in the title tag of the head.
I include my head in every page like this:
include 'includes/head.php';
This is my head:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
I thought this would work, but now it just shows "Head" on every page.
I know I can make it work by just putting the $page variable on every page but I would like to prevent this.
So is there any way to print the name of the current page through the included head.php file without adding anything to every page?
Thanks
EDIT
This is how I fixed it:
$page = pathinfo($_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME);
If you were to create a file head.php with the following content
<?php
$xpage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_BASENAME );
$ypage = pathinfo( $_SERVER['SCRIPT_NAME'],PATHINFO_FILENAME );
echo "
<!--
with extension
{$xpage}
without extension
{$ypage}
-->";
?>
and include in your regular php pages using include '/path/to/head.php' you should get the desired result ~ you will see two options - with or without file extension.
To add this to the document title simply echo whichever option is preferrable
<title><?php echo $ypage;?></title>
Try this enclosing the $page variable in php tags:
<head>
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?><title>
</head>
You have several problems, first one is curly bracket in front of the echo, the second one is that end title tag is missing forward slash and probably last one is that page variable is not inside php tags...
So your code should look like:
<?php $page = basename(__FILE__, '.php'); ?>
<title><?php echo ucfirst($page); ?></title>
You could use a function like this:
head.php
<?php
function head($page) {
echo "<head>";
echo "<title>".ucfirst($page)."<title>";
echo "</head>"
}
index.php
<?php
include 'includes/head.php';
$page = basename(__FILE__, '.php');
head(page);
Ok so I am making a website on each page where I want to include a file sidebar.php .
In sidebar.php I would like to echo the name of file that included sidebar.php .
Below are contents of sidebar.php, they return 'sidebar'.
<?php
$file = basename(__FILE__, '.php');
echo $file;
;?>
I found a similar question but the whole point is that I don't have to make no variables in on each page.
Excuse me for vague use of word 'include', I am using the it as the statement in php.
You could pass it to the sidebar page through a session variable:
<?php
session_start();
$_SESSION['filename'] = "thisFilesName.php";
include('../sidebar.php');
?>
sidebar.php:
<?php
session_start();
echo $_SESSION['filename'];
?>
I have php reading a text file that contains all the names of images in a directory, it then strips the file extension and displays the file name without the .jpg extension as a link to let the user click on then name, what I am looking for is a easy way to have the link that is clicked be transferred to a variable or find a easier solution so the link once it is clicks opens a page that contains the default header and the image they selected without making hundreds of HTML files for each image in the directory.
my code is below I am a newbie at PHP so forgive my lack of knowledge.
thank you in advance. also I would like a apple device to read this so I want to say away from java script.
<html>
<head>
<title>Pictures</title>
</head>
<body>
<p>
<?php
// create an array to set page-level variables
$page = array();
$page['title'] = ' PHP';
/* once the file is imported, the variables set above will become available to it */
// include the page header
include('header.php');
?>
<center>
<?php
// loads page links
$x="0";
// readfile
// set file to read
$file = '\filelist.txt' or die('Could not open file!');
// read file into array
$data = file($file) or die('Could not read file!');
// loop through array and print each line
foreach ($data as $line) {
$page[$x]=$line;
$x++;
}
$x--;
for ($i = 0; $i <= $x; $i++)
{
$str=strlen($page[$i]);
$str=bcsub($str,6);
$strr=substr($page[$i],0,$str);
$link[$i]= "<a href=".$page[$i]."jpg>".$strr."</a>";
echo "<td>".$link[$i]."<br/";
}
?>
</P></center>
<?php
// include the page footer
include('/footer.php');
?>
</body>
</html>
add the filename to the url that you want to use as a landing page, and catch it using $_GET to build the link.
<a href='landingpage.php?file=<?php echo $filename; ?>'><?php echo $filename; ?></a>
Then for the image link on the landing page
<img src='path/to/file/<?php echo $_GET['file'] ?>.jpg' />
I can't echo my variable above my CMS include code.. but if I echo the variable after, then it recognizes the $url variable.
Here is some code:
<?php
// here is my CMS inlcude code
$template = 'news_script';
$number = '';
$category = '';
include $cutepath.'/show_news.php';
?>
If I echo $url above the include code, it returns nothing. But below, it obviously recognizes it.
Is there a php function that scans the whole page and retrieves all the POST variables so you can use $url at the top of the php page with a header('Location:'. $url); script??
Obviously $url is being defined in your show_news.php script. PHP executes a script line-by-line, and will not magically "reach back" to set a variables value in an earlier line.
Another (ugly) way would be:
<?php
// here is my CMS inlcude code
$template = 'news_script';
$number = '';
$category = '';
ob_start();
include $cutepath.'/show_news.php';
$buffered_data=ob_get_contents();
ob_end_clean();
echo $url; // here you could place your: header('Location:'. $url);
echo $buffered_data;
?>