AD Posted September 29, 2008 Share Posted September 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/ Share on other sites More sharing options...
Adam Posted September 29, 2008 Share Posted September 29, 2008 should do if you change the extension to .php .. something.inc.php .. that should work... Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652882 Share on other sites More sharing options...
redarrow Posted September 29, 2008 Share Posted September 29, 2008 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652896 Share on other sites More sharing options...
AD Posted September 29, 2008 Author Share Posted September 29, 2008 Thanks for the reply MrAdam, I tried renaming the .inc file to what you suggested .inc.php but the result was the same. Any other ideas as to what the problem might be? Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652902 Share on other sites More sharing options...
Adam Posted September 29, 2008 Share Posted September 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652907 Share on other sites More sharing options...
AD Posted September 29, 2008 Author Share Posted September 29, 2008 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");?> Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652949 Share on other sites More sharing options...
thebadbad Posted September 29, 2008 Share Posted September 29, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-652972 Share on other sites More sharing options...
AD Posted September 29, 2008 Author Share Posted September 29, 2008 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");} ?> Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653051 Share on other sites More sharing options...
thebadbad Posted September 29, 2008 Share Posted September 29, 2008 It's pretty difficult to figure out when I haven't seen the whole code. And your setup is pretty confusing. Does the .inc page require main.php? So main.php requires a .inc, that requires main.php again? ??? Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653076 Share on other sites More sharing options...
AD Posted September 29, 2008 Author Share Posted September 29, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653106 Share on other sites More sharing options...
thebadbad Posted September 29, 2008 Share Posted September 29, 2008 Yeah, MrAdam's method is far easier to work with, I think. Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653363 Share on other sites More sharing options...
DarkWater Posted September 29, 2008 Share Posted September 29, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126265-changing-page-titles/#findComment-653368 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.