Jump to content

Recommended Posts

Hi,

 

I was hoping someone could help me out with this issue, I know nothing of php unfortunately.

 

My main.php page forms the structure of all my pages including the head section and basic layout. My .inc pages have the content and content structure.

 

The php for this is as follows

 

?php

$page = isset($_GET['page']) ? $_GET['page'] : 0;

$topic = isset($_GET['topic']) ? $_GET['topic'] : null;

if ($pic != null) {}

else {require_once("content/page_".$page.( $topic != null ? "_".$topic:"" ).".inc");}

?>

 

Is there a way for each .inc page to have its own title that is called along with the content?

 

I have tried the following in the main.php page:

 

<title><?php if(isset($title)) { print $title; } else { print "Default Title"; } ?></title>

 

and in the .inc page:

 

$title = "New Title";

require_once("main.php");

 

This works but only gives me the "Default Title" across all pages, not the "New Title". Do I enclose the .inc page code in ?php ---- ?> . Just groping in the dark here.

 

Any help would be appreciated

Thanks

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/
Share on other sites

could use session much easer mate....

 

There loads off ways my friend.........

 

my_functions.php

<?php session_start();

$_SESSION['title1']="this is the first title for the page']";
$_SESSION['title2']="this is the secound title for the page']";
$_SESSION['title3']="this is the thered title for the page']";

?>

 

 

page 1

<?php session_start();

echo $_SESSION['title1'];

?>

 

page 2

<?php session_start();

echo $_SESSION['title2'];

?>

 

 

page 3

<?php session_start();

echo $_SESSION['title3'];

?>

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652896
Share on other sites

Bit of a strange way of doing it (sessions)?

 

Could make it much more flexible and put all the code to output into a function, like:

 

template.php

<?php

function writeHeader($title) {
?>
<html>
<head>
 <title><?php print $title; ?></title>
 ....
...
...
<?php
}

function writeFooter() {
?>
 ...
 ...
</body>
</html>
<?php
}

?>

 

somefile.php

<?php

include 'template.php';

writeHeader('Homepage | Mysite.com');
?>
     <div>Your page content here...</div>
<?php
writeFooter();
?>

 

What I used once, and is fairly simple compared to something like SMARTY templates, or other template engines...

 

 

Adam

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652907
Share on other sites

Thanks redarrow and MrAdam for your input.

 

I wanted to do it this way because it seemed to be the simplest way without changing the structure of what is already there or adding a lot of code.

 

I keep on thinking that it is just a case of a wrong parentheses or comma or something simple like that which is causing it not to work as it seems to make sense

 

<title><?php if(isset($title)) { print $title; } else { print "Default Title"; } ?></title>

 

<?php $title = "New Title"; require_once("main.php");?>

 

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652949
Share on other sites

It should work, if the first code snippet is inside "main.php".

 

Another thing, your code is vulnerable to malicious attacks. Never include a file based on user input without sanitizing/filtering the input first. Use an array of allowed pages. Example:

 

<?php
$allowed = array('first', 'second', 'third');
if (in_array($page, $allowed)) {
include "content/$page.php";
} else {
echo 'Page not found.';
}
?>

 

This can only include first.php, second.php and third.php, no matter what the user might put into $page.

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652972
Share on other sites

Thanks TBB for the advice, will do so.

 

The first snippet is inside the main.php and the second in my .inc pages which have the content. The 'require_once' in the .inc pages is working because if I replace it with 'require' it then duplicates the page. However even though the 'require_once' seems to work I still get the 'default title' as opposed to the 'new title'.

 

Could there be a conflict with the other php code in my main.php which calls the .inc pages?

 

<?php
$page = isset($_GET['page']) ? $_GET['page'] : 0;
$topic = isset($_GET['topic']) ? $_GET['topic'] : null;
if ($pic != null) {}
else {require_once("content/page_".$page.( $topic != null ? "_".$topic:"" ).".inc");}
?>

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653051
Share on other sites

Yes the .inc requires the main.php for the head and layout. So yes you are right, bit of a circular argument going on. I didnt design the site, just update it and tweak it for seo and to make it more user friendly. I could use java to change the title but there are those who advise that this isnt a good idea as the title change would just be "cosmetic" and seen by the user only, not by the bots.

 

I would also like to change the urls to be text not string based. With all these changes what do you think the best option is? Perhaps MrAdam's advice?

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653106
Share on other sites

could use session much easer mate....

 

There loads off ways my friend.........

 

my_functions.php

<?php session_start();

$_SESSION['title1']="this is the first title for the page']";
$_SESSION['title2']="this is the secound title for the page']";
$_SESSION['title3']="this is the thered title for the page']";

?>

 

 

page 1

<?php session_start();

echo $_SESSION['title1'];

?>

 

page 2

<?php session_start();

echo $_SESSION['title2'];

?>

 

 

page 3

<?php session_start();

echo $_SESSION['title3'];

?>

 

 

 

 

 

That's an absolutely horrible way to do it.  o_O

Link to comment
https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653368
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.