Wednesday, February 22, 2012

Return Home
Contact Me

Soholaunch Current Page Function

Unfortunately, Soholaunch doesn’t store the current page the user is on in any of it’s many session variables. I use this function in pretty much every template I create for Soholaunch. It basically just tells you what page is being viewed by the user and is helpful for conditionally placing code in your template. For example, you can use it on a page that is going to have a photo gallery on it and you only want the JavaScript and style sheet to be called in the <head> portion of your template when the visitor is on that page.

The Code


<?php
function currentPage(){
  $temp=explode("/",$_SERVER['REQUEST_URI']);
  $thisPage=$temp[(count($temp)-1)];
  $thisPage=str_replace("index.php?pr=","",$thisPage);
  $thisPage=str_replace(".php","",$thisPage);
  $thisPage=str_replace("_"," ",$thisPage);
  $thisPage=str_replace("/","",$thisPage);
  $thisPage=(ereg("\?",$thisPage))?substr($thisPage,0,strpos($thisPage,"?")):$thisPage;
  if(!strcmp("",$thisPage)){
    $result=mysql_query("SELECT startpage FROM site_specs LIMIT 1");
    $thisPage=mysql_result($result,0,'startpage');
  }
  return $thisPage;
}
?>

The Breakdown

I use the Request URI in case the install is in a child directory of the account. For example, yourdomain.com/soholaunchdir/My_Test_Page.php. Next, I only need the last portion of the URI, so I assign my $thisPage variable to the last index of the array. If the user did not choose SEO friendly links then I need to strip away the index.php?pr= portion, if they do have it on then I need to strip away the .php portion. Next, replace all underscores with spaces and then get rid of the forward slash. The last string manipulation is in case there is a query string, as I don’t want that trailing on the page name. Finally, if the user is only at the domain name, http://www.yourdomain.com, the $thisPage variable will be an empty string, but I want it to say I’m on the start page set in the webmaster settings. So, a simple query to the database will tell me that and I’m done.

Examples

Using a page called My Test Page, would render the same result in the following examples:

URL: http://www.yourdomain.com/My_Test_Page.php

  1. $_SERVER['REQUEST_URI']: /My_Test_Page.php
  2. $temp[(count($temp)-1)]: My_Test_Page.php
  3. str_replace(“index.php?pr=”,”",$thisPage): My_Test_Page.php
  4. str_replace(“.php”,”",$thisPage): My_Test_Page
  5. str_replace(“_”,” “,$thisPage): My Test Page
  6. str_replace(“/”,”",$thisPage): My Test Page
  7. (ereg(“\?”,$thisPage))?substr($thisPage,0,strpos($thisPage,”?”)):$thisPage: My Test Page

Return Value: My Test Page

URL: http://www.yourdomain.com/index.php?My_Test_Page

  1. $_SERVER['REQUEST_URI']: /index.php?pr=My_Test_Page
  2. $temp[(count($temp)-1)]: index.php?pr=My_Test_Page
  3. str_replace(“index.php?pr=”,”",$thisPage): My_Test_Page.php
  4. str_replace(“.php”,”",$thisPage): My_Test_Page
  5. str_replace(“_”,” “,$thisPage): My Test Page
  6. str_replace(“/”,”",$thisPage): My Test Page
  7. (ereg(“\?”,$thisPage))?substr($thisPage,0,strpos($thisPage,”?”)):$thisPage: My Test Page

Return Value: My Test Page

URL: http://www.yourdomain.com/My_Test_Page.php?var=testing

  1. $_SERVER['REQUEST_URI']: /My_Test_Page.php?var=testing
  2. $temp[(count($temp)-1)]: My_Test_Page.php?var=testing
  3. str_replace(“index.php?pr=”,”",$thisPage): My_Test_Page.php?var=testing
  4. str_replace(“.php”,”",$thisPage): My_Test_Page?var=testing
  5. str_replace(“_”,” “,$thisPage): My Test Page?var=testing
  6. str_replace(“/”,”",$thisPage): My Test Page?var=testing
  7. (ereg(“\?”,$thisPage))?substr($thisPage,0,strpos($thisPage,”?”)):$thisPage: My Test Page

Return Value: My Test Page

Download

Click here to download the code.

Comments

Please post your comments, questions, or suggestions here.

* Denotes a required field.

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Recent Posts: