PHP and pagetitle

Soldato
Joined
24 May 2006
Posts
3,824
Location
Surrey - UK
PHP minor questions

Hi guys,

Wondering if anyone with PHP know how can tell me if they see a problem with handling a page's title like so.

header.php
Code:
<?php require_once ('title.php')?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Site Name<?php 
   echo $dir, $page;
?></title>
</head>
<body>
title.php
Code:
$ck = 1;

$page = basename($_SERVER['SCRIPT_FILENAME']);
$page = str_replace('_',' ',$page);
$page = str_replace('.php','',$page);

if (strlen($page) > $ck) {
$page = ' - ' .ucfirst($page);
} else {
$page = '';
}

$dir = $_SERVER['PHP_SELF'];
$dir = str_replace(basename($_SERVER['SCRIPT_FILENAME']),'',$dir);
$dir = str_replace('/','',$dir);

if (strlen($dir) > $ck) {
$dir = ' - ' .ucfirst($dir);
} else {
$dir = '';
}
Each of the pages includes the header and footer, etc....

This way i get my titles as "Site name - Directory - Filename"...

The code functions already, i'm just after a few second opinions please.. :)
 
Last edited:
Ok, if i can't trust $_SERVER for grabbing the file location...hmmm....

Still learning at the moment, and the $_SERVER method works on both local and live installs.

Also works for creating a unique body ID to...

So whats the PHP alternative for simple checking file name and current directory, if any, as i was under the impression the $_SERVER call was valid.

How about?
$dir = dirname(__FILE__);
for directory....

Can't find an alternative for file name though....
 
Ok few small questions for anyone that can answer.

When you use MySQL alongside PHP do you have seperate mysql users setup for different tasks.

There are times when you may wish to just select tables from a database, at this point to you use a connection to that database with limited privileges to SELECT from?

The question really is, do you have different users in MySQL for different taks, ie, one for selecting, one for updating, one for inserting, and so on....

Been working on bits of PHP over the last week or so, seem to be getting the jist of it more so now then i had before.

So far i can pull news from mysql, grab all articles for the main page, or grab a single article when an ID is applied, ie. /news/index.php?id=10 , where 10 is the ID stored in the DB for that article. I can strip the HTML and convert [tag] into HTML (not yet required but had a quick play) using preg or str replace.

It's all just for fun, so i'm enjoying playing with the various PHP commands, but the MySQL is the most challenging in terms of how i could/should be setting up privs. Queries seem to be simple enough, PHPMyAdmin can create the queries in most cases, then just a little tweaking and job done.

Open to any suggestions or advice..... links welcome, though generally php.net is my reference, the examples are easier in most cases, though some are not defining or explanatory enough it seems more current then a lot of the guides i've found around the web.
 
Ok forget the other posts, i have all those bits covered now.

Having some problems with strip tags now, can get my news posts to strip tags from the news text, but not the title etc...

So for example i can plonk the following into the news title and text...

Code:
<a href="examplelink">example</a>
The title with return as a clickable link where as the content/post will return just the example text.

Here's a snippet from the code that display the news post....

Code:
        while ($row = mysql_fetch_array($arti)) 
        {
            $row['title'] = strip_tags($row['title']);
            
            echo '<ul class="nws"><h1>'.ucfirst($row['title']).'</h1>
            <li class="info">by '.$row['author'].' on '.date("y-m-d", $row['date']).'</li>
            <li>';

            $row['article'] = strip_tags($row['article']);
            $row['article'] = ucfirst($row['article']);
            
            echo bbcode($row['article']);
            echo '</li>
            </ul><br />
            ';
        }
Now thinking about it, i would assume that's because i should be stripping the content when it's being placed into the DB and not when i'm extracting from it.....

I'll be at it for hours in any case, any suggestions welcome...

EDIT:
I should post every time i get stuck, seems i always figure it out once i post.... :confused:

Stripped the content on the post form before it inserts into the DB.... :) All working now....
 
Last edited:
It's working fine, i only needed to strip the data when it was being passed to the DB rather then relying on it getting stripped on the extraction which is where i was going wrong.

I have bbcode written into the news post display, so i've no need for HTML to be parsed in the news post form. Plus if anyone exploits my beginner PHP attempts it will hopefully make things harder on them if the page keeps stripping out the tags....:)
 
I tried to make a post on my Wordpress with the title:



Guess what happened.

Not a problem in window titles though I expect.


I'd imagine you received 'a is a'..... or you received a 'HTML is not allowed in title' or a message to that degree.

Most CMS systems have HTML stripped from posting, or at least any that i've used, but that's not something that just happens as standard, i'm coding my own with google as my reference so i'm using str_replace and strip_tags etc... for filtering the data...

HTML is getting stripped and so are slashes, but now i'm stuck figuring out how to stop the str_replace removing slashes from my bbcode style functions but not otherwise.

Lots of ways to strip the data, some ways work better then others, but some are removing legitimate data when it's not wanted to....

READ, READ, READ.... i'll get there....

Next on the agenda is checking over the SESSION method i'm using which was a free copy and paste job to get me started....
 
Back
Top Bottom