Jump to content

echo text based on the url


PlagueInfected

Recommended Posts

im trying to code this little script to where if you're on this page, you echo this and if you're on that page you echo that text.

 

here is the current code i tried and no luck with it, any ideas?

 

<title>

<?php 
$page = array();
$page = explode("/", $_SERVER['PHP_SELF']);
$directory_no = count($page) - 2;
$directory = $page[$directory_no];
define (DIRECTORY, $directory);

if (DIRECTORY == '/') {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

elseif (DIRECTORY == '/?pg=index') {
    echo 'Plague Infected | Index';	
}
elseif (DIRECTORY == 'contact.php') {
    echo 'Plague Infected | Contact';	 
}

elseif (DIRECTORY == 'portfolio.php') {
    echo 'Plague Infected | Portfolio'; 	
}
else {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

	?>
</title>

Link to comment
Share on other sites

$page = array();
$page = explode("/", $_SERVER['PHP_SELF']);
$directory_no = count($page) -1;
$directory = $page[$directory_no];

 

this line: $directory_no = count($page) - 2;

 

should be -1, because you want to get the last entry in your exploded array (which would be the page itself) -2 will give you the second to last, and in many cases, that is also the first, and sometimes it will become -1.

 

 

Hope that helps

Link to comment
Share on other sites

like this...

 

<title>

<?php 
$page = array();
$page = explode("/", $_SERVER['PHP_SELF']);
$directory_no = count($page);
$directory = $page[$directory_no] -1;

if ($directory == '/') {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

elseif ($directory == '/?pg=index') {
    echo 'Plague Infected | Index';   
}
elseif ($directory == 'contact.php') {
    echo 'Plague Infected | Contact';    
}

elseif ($directory == 'portfolio.php') {
    echo 'Plague Infected | Portfolio';    
}
else {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

      ?>
</title>

Link to comment
Share on other sites

it will echo

 

Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico

 

and i added my site's URL and it still gave me the first tag

 

print_r doesn't seem to do it, here is how i coded it

 

<?php 
$page = array();
$page = print_r("/", $_SERVER['SCRIPT_NAME']);
$directory_no = count($page);
$directory = $page[$directory_no] -1;

if ($directory == 'http://plagueinfected.com/') {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

elseif ($directory == 'http://plagueinfected.com/?pg=index') {
    echo 'Plague Infected | Index';   
}
elseif ($directory == 'http://plagueinfected.com/contact.php') {
    echo 'Plague Infected | Contact';    
}

elseif ($directory == 'http://plagueinfected.com/portfolio.php') {
    echo 'Plague Infected | Portfolio';    
}
else {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

      ?>

Link to comment
Share on other sites

this is not going to work. $_PHPSELF returns the page your are currently on, sans any paths to the document itself. So if i'm on www.facebook.com/books/awesome.php and I echo $_SERVER['PHP_SELF'] it will echo "/awesome.php" You are trying to test the variable against a full URL and it will never ever match those.

 

none of those will work. And Echo the variable itself. As in change your code to

 

$page = array();
$page = print_r("/", $_SERVER['SCRIPT_NAME']);
$directory_no = count($page);
$directory = $page[$directory_no] -1;
print_r($page);
echo $directory;

if ($directory == 'http://plagueinfected.com/') {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

elseif ($directory == 'http://plagueinfected.com/?pg=index') {
    echo 'Plague Infected | Index';   
}
elseif ($directory == 'http://plagueinfected.com/contact.php') {
    echo 'Plague Infected | Contact';    
}

elseif ($directory == 'http://plagueinfected.com/portfolio.php') {
    echo 'Plague Infected | Portfolio';    
}
else {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

 

that will tell you what the actual variable itself looks like, and what you should change your test too

Link to comment
Share on other sites

tried it again, it only pulls -1 than the tag 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'

 

so it looks like this

 

'/-1lague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico'

 

$page = array();
$page = print_r("/", $PHP_SELF);
$directory_no = count($page);
$directory = $page[$directory_no] -1;
print_r($page);
echo $directory;

if ($directory == 'http://plagueinfected.com/') {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

elseif ($directory == 'http://plagueinfected.com/?pg=index') {
    echo 'Plague Infected | Index';   
}
elseif ($directory == 'http://plagueinfected.com/contact.php') {
    echo 'Plague Infected | Contact';    
}

elseif ($directory == 'http://plagueinfected.com/portfolio.php') {
    echo 'Plague Infected | Portfolio';    
}
else {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

Link to comment
Share on other sites

oh didn't notice something in your code above

 

when I said print_r I meant

 

print_r($page);

 

not to do

$page = print_r("/", $PHP_SELF);

 

that is messing up your variables. change it back to

 

$page = explode("/", $PHP_SELF);

 

or whatever it was before

Link to comment
Share on other sites

ok done it

 

still echos the first line

 

dont know if i did it right with the calling

 

<?php 
$page = array();
$page = explode("/", $PHP_SELF);
$directory_no = count($page) - 1;
$directory = $page[$directory_no];

if ($directory == '/') {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}

elseif ($directory == 'index') {
    echo 'Plague Infected | Index';   
}
elseif ($directory == 'contact.php') {
    echo 'Plague Infected | Contact';    
}

elseif ($directory == 'portfolio.php') {
    echo 'Plague Infected | Portfolio';    
}
else {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}
      ?>

Link to comment
Share on other sites

elseif ($directory == 'index') {

 

needs to be index.php

 

if ($directory == '/') {

doesn't mean anything and will never actually run true. You are only seeing this line because it is also echoed in the else clause.

 

I would set it up like this:

 

<?php 
$page = array();
$page = explode("/", $PHP_SELF);
$directory_no = count($page) - 1;
$directory = $page[$directory_no];

if ($directory == 'index.php') {
    echo 'Plague Infected | Index';   
}
elseif ($directory == 'contact.php') {
    echo 'Plague Infected | Contact';    
}

elseif ($directory == 'portfolio.php') {
    echo 'Plague Infected | Portfolio';    
}
else {
    echo 'Plague Infected Designs | Freelance art and web design and development in Fort Worth TX and Puerto Rico';
}
      ?>

Link to comment
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.