I construct my webpages like this:
<?php
include ('header.html');
?>
<p> bla bla bla </p>
<?php
include ('footer.html');
?>
I use session in the header.html to track the user records. The header.html has got three tags, called logo on the left, search_form in the middle, and login/ sign up buttons on the right
Now, the question is that I would like to hide the search_form DIV in the middle by using php in the header.html file if and when the internet users (not registered members) visit the certain page, e.g., register.php, because this DIV is not necessary.
Note: I would like to control it using PHP, but not javascript or anything else.
Can you help me, please?
Thanks
You could use $_SERVER['REQUEST_URI'] to check if the current url is, for example, 'register.php'. Something like:
<? php
if ($_SERVER['REQUEST_URI'] == 'mysite.com/register.php'):
include ('register_form.php');
?>
Of course, this would only work if the url matched exactly, and query strings would break this. But you get the general idea.
Look into creating sessions and user authentication using PHP.
Change your header.html page to header.php.
in header.php check that if current user as a registered user then show the search div or
check current page by your browser url and make decision whether you want to show search div or not.
You can use session to get current user information in header.php
sample code:
header.php
session_start();
if(isset($_SESSION['current_user'])){ //check register user
// show div
}
else {
$page= $_REQUEST['current_url']; // get current page
if($page == 'register.php' || $page == 'whatever' ){ //check pages where you are not show search page
echo '';
}
else {
//show div
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php
session_start();
$_SESSION['view'] = $_SERVER['REQUEST_URI'];
if(isset($_SESSION['view']) && $_SESSION['view'] == '/test/index.php') {
?>
<div>head</div>
<?php } ?>
body here
<div>footer</div>
</body>
</html>
This is my sample work and this is works correctly. If my url not equal to /test/index.php it does not show head. You can set your own logic according to this demo work. I think it can help you.
I tried and tried and found out the solution already. Now, I can share it with you guys:
if( (isset($_SESSION['current_user'])) || (basename($_SERVER['PHP_SELF']) != 'register.php') ){
// show the search_form DIV
}
Thanks for your contribution!
Related
Hi guys I am making a website for a school project, but to keep in organised, I want to have all pages in one document, and when you go to the site the default page is index.php, and the other pages are linked like this: index.php?p=page
the problem now is, that I had this script, but don't know where I have it. I'm not skilled in PHP too, I got it from a friend of mine. It was like this:
?>
<php? some php code ?>
<html tag etc>
<head> Head stuff </head>
<Nav> <Header>
<php? some php code where I filled in what the link was of the content under it ?>
< The content of the page, excluding the header, navigator etc because that was above here. >
?>
Then I could do the same thing over and over again, and just change the PHP code and it worked.
<footer stuff>
</body>
</html>
I know that there are many scripts for this, and I searched Google for over an hour, but with no succes.
-Tristan
A very basic example of how it works :
<?php
$page = $_GET['page'];
if ( $page == 'one' ) {
echo 'This is page one!';
} elseif ( $page == 'two' ) {
echo 'This is page to';
}
// http://yoursite.com/index.php?page=one
// Outputs 'This is page one!'
Learn little more about GET request right here : $_GET
This will help you:
Serve multiple pages from 1 PHP file?1
If you want to make something like youtube.com/?watch=XXXXXXXXX
so you need just to change XXXXXXXXX to display multiple content.
but you still can use :
One-page navigation system.
Display / Hides Pages With JS
Example Of JavaScript:
we have website1 as a website
we have a - b - c - d as pages
js will hide all pages exept the page you want to show.
I'd like to know if it is possible to add content to the <head> section of HTML from the <body>, in PHP.
I know how to do this in JQuery, but I need to the final HTML code to already have the aditional data.
I also know that I can use ob_start() to manage all the content that will be processed, but the reason I'm asking this is because I have some websites already finalized and wanted to know if there is a simpler way to do this, instead of restructure the entire website (and it isn't just one, there are several).
Just for explain better, usually my websites are structured like this:
- I have a "master page", who have all the content there are present in the whole site (like header, footer, etc), kind of like this (it's not this simple - I use validation, friendly URLs, etc - it's for example only):
<?php
$page = $_GET['page'];
<html>
<head>
// All my header content
</head>
<body>
if($page == 'home')
include('pages/home.php');
elseif($page == 'about')
include('pages/about.php');
// etc
// Here comes my footer content
</body>
</html>
?>
Bottom of line: I want to add content to the <head> from some of the pages included.
Since now, thank's for all help.
Maybe you can look at the concept of the code below. What you really want is not clear for me so you can add comment if ever.
<?php
$msg = "<script> function TryOpen(){ alert('Hello World!'); } </script>";
?>
<html>
<head>
<?php echo $msg; ?>
</head>
<body>
<?php
if($page == 'home')
include('pages/home.php');
elseif($page == 'about')
include('pages/about.php');
?>
<input type="button" name="btnAlert" value="Click" onclick="TryOpen();" />
</body>
</html>
For I have no idea what kind of content you want in the <head> tag so I made a script for an example. Hope it can somehow help.
I have a site lets name it http://mysite.com/index.php which is my main site with all the information and I want to have another page which is something like http://mysite.com/index.php?=premium and in this premium page will be totally new whole page. Which means not the same title or not have anything relate to the index.php page.
The reason is that I don't want my visitors to go directly to the premium page so they have to come to the index.php page first then click it from there.
Is that possible this way?
#knittl well not sure if my question was cleared
but what is actually happening now that the above code can get me to the premium page fine with no problem
but inside the premium page include many div class and I have this in the premium page
<?php
if(!isset($_GET['ch'])){
$ch = 1;
} else {
$ch = $_GET['ch'];
}
if ($ch == 1) {
echo "<div class='player_live_server_info'>
so what happen now that I have this
<li><img src="live_img/ch_5.jpg"></li>
I don't know if I do it right or wrong but It just redirect back to the normal page.
<?php if(isset($_GET['page']) && $_GET['page'] == 'premium') {
// this is the premium version, only visible when called as index.php?page=premium
?>
<html>
<head><title>you are on the fancy premium page</title></head>
<body>
<h1>your premium page</h1>
</body>
</html>
<?php } else {
// this is the normal page with a link to the premium version
?>
<html>
<head><title>you are on the lame normal page</title></head>
<body>
<h1>your normal page</h1>
<p>
click here to go to our premium page
</p>
</body>
</html>
<?php } ?>
$_GET['something'] is the best choice for you..you can also use $_SESSION, but that will be complicated plus make your application slower.
I have many sub pages, there urls like www.domain.com/sub/page1.php www.domain.com/sub/page2.php... Now when I type there url in browser. they all redirect to www.domain.com/sub/index.php, and the current sub page will show in a div dom in the index.php.
My knowledge is very limited. I only know jqeury.load and php header Location. but it is difficult redirect to index.php then tell index.php, which is the current sub-pages then do a jqeury.load.
One more note: Also should think SEO. maybe jqeury.load is bad for a search spider. maybe should use hash url.
So I ask for a help. is there anybody could give me some good suggestion? or simple worked examples?
Thanks.
In every PHP file (e.g page1.php) use this code:
<?php
if(!defined('INCLUDE')) {
header('Location: index.php?site=' . $_SERVER['PHP_SELF']); //Send the user to the index.php page
die();
}
?>
Insert the content you want here...
And in the index.php file:
<?php
define('INCLUDE', true);
if(isset($_GET['site'])) {
$site = $_GET['site'];
} else {
$site = 'page100.php'; //Put the name of the default page if the user visits index.php here
}
?>
<html>
<head><!-- HEAD CONTENT HERE --></head>
<body>
<div><?php
include($site); //Put the page requested into the div
?></div>
</body>
</html>
I've been using headers to create templates for websites.
It's easy, and very convenient for debugging.
I now face the problem of using head BUT with custom page titles.
If this is my header.php >
<html>
<head>
<title> My Site : ??? </html>
</head>
<body>
</body>
</html>
I need ??? to be replaced for every page.
Is this possible? If so, how? Thank you. : )
Not knowing more about your file inclusion scheme, the simplest way would be:
page2.php
<?php
$pageTitle = 'Page 2';
include 'header.php';
?>
<div>My content</div>
<?php include 'footer.php'; ?>
header.php
<html>
<head>
<title> My Site : <?php echo $pageTitle ?> </title>
</head>
<body>
footer.php
</body>
</html>
webbiedave's answer is perfectly fine, but in the long run, you should really learn to use either a decent template language (Smarty, Twig), or a PHP framework that has it's own templating. Kohana and Codeigniter are both pretty easy to get into.
If i were to add some code before including the header, will it help?
<?php
$currentPage = "Random Page Title";
include "header.php";
?>
And then use the value in header.php so print the page title?
you could query a DB for the title of a page and then print it using php :)
Edit:
Looking back at the problem , depending on how you have your website designed this may not be the simplest solution. But if you are already using some sort of ID system this should be easy.
Yes, it will help definitely. But you need to do a little customization.
First of all, make sure that you connect to the database, if you want to query / fetch data from database. For this to happen, include the "config.php" page at the very beginning of the script, in which your database connection logic will be present.
Then, write your query to fetch data from that database, and assign that value to the required variable for using it in the header page.
Lastly, include your "header.php" page.
For "config.php" page:-
Logic of Database Connection, like using of "mysql_connect()" & "mysql_select_db()" functions.
For "custom.php" page:-
<?php
include "config.php";
$sql = "SELECT pageTitle FROM db_table WHERE condition = 'something'";
$sql_exe = mysql_query($sql) or die("Error in Fetching Page Title");
if( mysql_num_rows($sql_exe) ) {
$currentPage = mysql_result($sql_exe, 0, 0);
}
else {
$currentPage = "Random Page Title";
}
mysql_free_result($sql_exe);
include "header.php";
?>
Also, if you want, you can always use some class for mysql connection & queries, to fetch data. But this is how it always work.
you can call javascript to change the title of the page dynamically, this is a better solution if you have a master file index.php that calls all other includes
You could also use something like this:
if you married up your php filenames with your page titles
you could use explode or str replace to make it more user friendly to replace commas or underscores for example.
<?php
echo basename($_SERVER['REQUEST_URI']);
?>
or
<?php
// my_page_title.php
$var=basename($_SERVER['REQUEST_URI']);
$pagetitle=str_replace("_"," ",$var);
// my page title
?>
<title> My Site : <?php echo $pagetitle; ?> </title>
Corrected 1 small error in Webbiedave's header.php entry
</html> should be </title>
<html>
<head>
<title> My Site : <?php echo $pageTitle ?> </title>
</head>
<body>