How do you get the current directory name in PHP?

July 1, 2009

I ran into a situation where I wanted to get the name of the directory I was in, in PHP. To be clear, I didn’t want the full path, just the directory/folder name

To be clear, if I was working in the directory:
/home/username/public_html/addonname

I wanted to have addonname returned.

I worked out two solutions.

The first solution used the getcwd() function which returns the full directory path as shown above but to use the basename function to get the part I needed. It worked so… “echo basename(getcwd());” returns “addonname” in this scenario.

That would meet my needs perfectly. I believe that matches my requirements in spirit, but was not literally correct. That statement returned the current working directory, but not the directory where the file was located. In fact, PHP 4 and PHP 5 differ when getcwd() is called from the command line. If you are in the ‘/’ directory and execute “php /test/talktome.php“, a php 4 getcwd() in that file will return the path “/test” while PHP 5 will correctly return ‘/’.

To resolve this, this next call works even better:
echo basename(dirname(__FILE__));

Does anyone want to do some speed tests to see which is faster after 10,000 calls? Let us know your results.

WordPress Quick Tip: Fixing the number of comments per post

January 7, 2009

For various reasons, sometimes the number of comments shown under the title of the post may not match the number of actual comments displayed under the post.

Here is a simple SQL statement that will resolve the issue:

update `wp_posts` set comment_count = (select count(*) from wp_comments
WHERE `comment_post_ID` = `ID` and comment_approved = '1')

You can use this to verify what would change and where your problems may lie:

SELECT ID, `post_title`, `comment_count`,
    (select count(*) from `wp_comments`
        WHERE (`comment_post_ID` = `ID`) and (`comment_approved` = '1')) as NewCC
    FROM `wp_posts`
    WHERE `comment_count` <> (select count(*) from `wp_comments`
        WHERE (`comment_post_ID` = `ID`) and (`comment_approved` = '1'))

PHPMyAdmin announces drop of PHP 4 support.

October 2, 2007

I was just over at the PHPMyAdmin site and saw this quote:

 Welcome to phpMyAdmin 2.11, which will probably be the last series supporting PHP 4.

Notice the ”probably” stuck in there.  They are testing the waters.

If phpMyAdmin is dropping the whole PHP 4 line, it just might push the WordPress adoption time a bit further.  Where phpMyAdmin goes, I have to think,  the ISPs will not be slow to follow.

How to have a central VCL for PHP location for all your websites

April 3, 2007

I have a number of sites that on which I want to use the VCL for PHP library components with Delphi for PHP creations.  I don’t want to waste uneeded space for a bazillion copies of the VCL.  As far as that goes, I don’t want to have to sit through the deployment wizard every time either.

You could edit PHP.ini and add the location to your include path, but the VCL for PHP is MORE than just a textual code libary.  It includes image files as well, for things like the dropdown arrow in the combo boxes.

 So what’s a person to do?  Well, it’s simple.  Create some symbolic links!

Just put the VCL in your root directory or at least one that can be accessed with a common path from the your website directories.  I use the root since I can use ~ to get at it easily on linux.  It could just as easily be /usr/local/lib/vcl or d:\VCL.  Then in your directories you just put two symbolic links, one named vcl and the other named vcl-bin. Both pointing to the vcl directory you made.

Here’s the step by step for *nix systems:

  1. Upload the whole VCL folder to the root web directory
  2. Telnet/ssh into the account
  3. Change to the directory for your website
  4. Run these two commands
    1. ln ~/vcl
    2. ln ~/vcl vcl-bin
  5. Repeat steps 3 and 4 for all of your sites.
  6. When you deploy, just grab the files in your project directory and upload them.  No worrying about the VCL any more.  You should be able to make a batch file that does that step!

That’s it (oh and just in case your font makes “ln” hard to readand that is a lower case “LN” as in link.)

 On Windows, you should be able to do the same thing.  If you have Vista you have native support for symbolic links.  And in XP, you can use NTFS links.  But I’ll leave it up to you to look up those technologies.  With that sort of thing, if you aren’t able to find and digest how to do it, you’re probably better off just using the deploy tool.

 Hope this helps someone!  It’s made my life a little easier.

Windows Implementation

Kevin Berry came up with this windows implementation:

For my Windows 2000 webserver, which I have total acces to, below is how I followed Brian Layman’s advice to made this work.

1. Downloaded linkmagic.exe from

http://users.pandora.be/jbranders/linkmagic.exe

2. Ran linkmagic.exe (setup program) on my webserver.

3. Launched Junction Link Magic program.

4. Created empty “vcl” folder in my clock project folder in my webserver root (c:\web\htdocs\clock\vcl) and selected this vcl folder as my junction folder.

5. Selected my true vcl folder loaded with vcl files as my destination folder (c:\web\vcl)

6. Clicked “create” button

Now my D4PHP clock sample program runs just fine in c:\web\htdocs\clock with only one main vcl installation on my webserver.

I suppose I could use the Junction Link Magic program to create new vcl junction folders in other vcl project subfolders on my webserver, but I discovered that once the first vcl junction folder is created, I can create more of these just by copying an existing vcl junction folder into a new project folder and choosing cancel when the file overwrite prompt appears. If I delete a vcl junction point folder, the real folder and its files don’t get deleted.

I for one don’t want different versions of my vcl all over my webserver in different folders (assuming I’ll have lots of web projects someday). For me it’s just simplest knowing the latest and greatest vcl library is always in one spot on my web server.

I don’t know if one could duplicate the directory structure of a windows hosted web from a remote server on one’s own computer to create the junction folder and then ftp that junction folder out to the remote server and preserve the redirection. If so, that would be great.

For my webserver, this is pretty nifty, Thanks again to Brian for the workaround until Codegear/Qadram figure out what I hope to be a better vcl deployment method.

Alternative Solution

David Plock also suggested another methot that uses a VCL-BIN alias. Jose Leon Serna provided the final fix to get this working. Kevin has done a great job of writing up these steps as well:

c:\web\htdocs and I wanted my VCL folder outside the root, so I made c:\web\vcl.

2. In my Apache httpd.conf file, I added the following alias line and directory permissions. For now, I just copied the permissions the icon
folder uses a few lines up. Anyone more enlightened is welcome to suggest a more appropriate permission set for the vcl folder. And of course, if you
put your vcl folder inside your web server's root directory, you can skip the directory permissions.

Alias /vcl-bin "c:/web/vcl"


Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all

3. In my php.ini file I added the following so that php will know in which folder to find the vcl folder.

include_path = “.;c:\web”

If I had dropped my vcl folder into my root directory on my webserver, the entry would have been:

include_path = “.;c:\web\htdocs”

4. Jose added the final piece to make this work. In my vcl.inc.php file I modified a line so that the alias name gets passed properly to my webserver. The line that mentions vcl-bin needs a forward slash in front of vcl-bin so the final line appears like this:

if (!array_key_exists(‘FOR_PREVIEW’,$_SERVER)) $http_path=’/vcl-bin’;

5. Make sure all configuration file changes above are saved and then restart the webserver.

Now you can drop D4PHP projects in any subfolders or even subfolders of subfolders of your root directory on your webserver and they work.

The nice thing about this solution in addition to it being a single vcl deployment is that it seems to me any web hoster can implement it once for
the entire web server and any subscribers can simply ftp D4PHP web files to their own folders on the server and they’ll work.

Thanks Brian, David, and Jose for two ways to make a single vcl deployment work!!!

If you want to see this solution in action, you can visit these links (on my slow webserver) for as long as my internet service provider leaves my ip address alone. The vcl folder is outside the web root and only clock.php, clock.xml.php, and background.gif are in each of these folders.

http://24.2.90.16/clock.php

http://24.2.90.16/clock/clock.php

http://24.2.90.16/clock/clock/clock.php

http://24.2.90.16/clock/clock/clock/clock.php

There you are. Two different methods proven successful in the field! Enjoy!

Delphi for PHP Article 3: A Brief History of Time and Delphi for PHP

March 20, 2007

When I was first fiddling around with the beta test stuff for Delphi for PHP, and before I’d gotten BetaBlogger status, I’d put together a post with what history I’d been able to scrounge about D4PHP from the WayBack Machine and Google.   I’d never gotten around to posting what I’d found.  Before it disappears forever into the nether regions of my harddrive, I thought I would hit Paste and Publish. 

I think I’ve gotten most of the details correct, but there are some guesses that might be inaccurate. Also, there are loads of links because I wanted to make it VERY clear that this info was gathered from the web and I was not breaking my NDA.  That’s not a concern anymore, luckily. You might find some of the links interesting. 

 So, here’s the history of Delphi for PHP as I know it:

Read more

Delphi for PHP Article 2: A Lazy Sunday (Video)

March 18, 2007

OK, I just got back from church and I am stuffed from the three bowls of soup and a huge salad.  So, I am feeling lazy, but I still want to get a Delphi for PHP article out (since I have a limited exclusion from my NDA and am alowed to discuss such matters or so I must say in each of these articles.)

 So, today you get treated with a video I made for the other Delphi for PHP testers.  It isn’t too exciting, but it will show you a little bit about the underlying structure of the DB Demos in the beta releases of Delphi for PHP.  I should say that it is entirely possible that the demos requiring the steps described in this video will be reworked for the final product.  If that’s the case you likely won’t have to do much, or ANY of this process in the full blown 1.0 process.

If you are new to PHP programming, you can use this process to create a webserver on your local home computer.   That’s downright handy for testing any web product.  So, you’ll benifit that way even if the Delphi for PHP stuff isn’t too handy for ya.

 OK here you go: 

The Delphi for PHP Beta Bloggers are on their way!

March 17, 2007

I was granted my Delphi for PHP BetaBlogger priveledges back on March 1.  I was the first to have that priveledge, but it was done specificaly so that I could have discussions with the WordPress folks at Automattic regarding a possible joint CodeGear/WordPress project for the 2007 Google Summer of Code™.  Unfortunately, with everyone crunching for possible product releases while the CodeRage conference was happening, the logistics could not be nailed down.  (BTW, that’s no problem.  What it REALLY means is that it openned the door for several other opportunities.  The first of which I’ll announce tomorrow.  Of course Matt‘s and Lloyd‘s readers already know all about it…) 

Though I had the legal rights to discuss the Delphi for PHP project with you, the BetaBlogger program had not officially started and I didn’t want to approve my priviledges.  So, while I did release one video and did another for the testers, I’ve kept mostly quiet until the full Beta Blogger program began.  Well that day is almost here.  

The WordPress for PHP BetaBlogger is about to start in earnest and I plan to be on top of this thing.  And I need your help to pull that off.  (Oh yeah, I should say in so-many-words that I am allowed to discuss this with you because I have a limited exclusion from my Delphi for PHP Non-Disclosure.  You will now be returned to your regularly scheduled program, already in progress)

Here’s my proposal: I will inform you of EVERY new D4PHP BetaBlogger post I hear spoken about behind the closed doors, and you all, come back here to this post and leave a comment for every one YOU see.  In return I’ll continue to put out Delphi for PHP videos and samples (as I have time) until you know this great new PHP tool inside and out.

Does that sound like a plan or what?  Is it a deal?

Delphi for PHP Article 1: A Video Walk Through

March 10, 2007

Rumors have been flying around for the last month about Borland producing a PHP product. “PHP? Are they nuts?” some said, “Concentrate on what you know! Are you trying to create another Kylix just to have it fade away to nothing?” Needless to say, I was a bit more optimistic than that… I mean come on! RAD PHP development? How cool would that be? PHP has the EXACT same feel as Pacal did back in the late 80s. It’s a baby language just starting to grow up. Even the people in it have the same core “I’m gonna help you because I know later you will help me” attitude that Pascal and Delphi always had.

Plus, since the restructuring of Borland has sifted the chaff from the wheat, CodeGear seems have its act together and the people of CodeGear are really the cream of the crop. (Yes, I know. I just said that they are Cream of Wheat, let it go… just let it go.)

Well, dreams came true when I was invited to be a CodeGear field tester. And last Thursday morning, with the verbiage hot off the presses from the Borland legal department, I was granted a limited exemption from my NDA (Non-disclosure agreement) to pitch a possible Google Summer of Code 2007 joint D4p/WordPress project to Automattic (I’ll let you know later how that turns out…). That NDA exemption also gave me permission to post articles and write reviews of the product. So here we are today!

So, now I can show you tell you all about the cool stuff I’ve been doing and show you movies I’ve made of the product in action.

In my usual fashion, I’ve written a gargantuan article. Instead of posting huge thing, I’m splitting the sections off into separate articles. Today, I just want to give you walk through of the product and show you some of the stuff that makes it unique.

This video uses a pre-release version of Delphi for PHP. Components, look and feel, and features may change before release. THIS VIDEO HAS SOUND.

If you have any problems with the video playback on any browser please let me know and I will try to get it to work.

Build your own spam filter with PHP and DNSBLs

December 2, 2006

Have you ever gotten an email asking if want certain parts of your body enlarged, parts that you might not even have? Was the next email you read one asking if you want to loose the inches you’ve recently gained? Did you ever notice how these emails are always from people that you are fairly certain have nothing to do with the contents of the email. Did MTeresa@Vatican.org really send that diet pill email? Have you ever gotten returned or rejected “can’t be delivered” emails from addressed you’ve never ever sent an email to?

I have.

SPAM. It’s HORRIBLE! My email box for Brian@TheCodeCave.com probably gets 3 to 1 spam over real email. I expected that. I put that address out everywhere and don’t protect it. It is meant to be my public address. But the FROM addresses on all that email never indicates who the email is really from. Even the company information inside the email header is faked. The spammers will grab someother name on their spam list and use it as their from address. I’ve had my name put into the from address of emails a few times. It’s an annoying problem, just ask the Nuclear Moose.

Why this can happen is a long story. It all relates back to the fact that SMTP and port 25 were never meant for submitting emails to email servers. SMTP was only meant for server to server communications. However, that’s for a different post. The long and short of it is that everything can be faked except for one thing: the IP address of the server that sent the email.

Because that IP address is accurate, you can use it to tell if the person that sent the email is a spammer. The post tells you a couple ways to do that. And because this is The Code Cave, you get a fully functional php routine to boot.
Read more

How does ROT13 encryption work?

December 1, 2006

I’ve added a new page that demonstrates ROT 13 encryption.

You can see it here:
http://ROT13.TheCodeCave.com

Next Page »

Who is Brian Layman

I am a WordPress expert living in North East Ohio. I am part of the ever expanding Open Source Internet workforce. I am able to stay at home, with my wife and four home schooled kids, while working as the Senior Developer for b5media - a blogging network that has hosted over 300+

I co-host the NEO WordPress Monthly meetup. I am the board chair of our local church. I host and have provided development services for clients such TV personalities Rhett and Link as well as corporations such as Borland International.

In my spare time I try to sneak out, canoe, mountain bike and camp as often as I can. Sometimes I also defend the earth against zombies and aliens, but usually not during the camping trips.

Services Provided

In providing hosting, email, theme and plugin development to my clients, I function as a single point of contact answering to the needs of their expanding sites.

My service portfolio includes but is not limited to WordPress hosting, optimization, theme development and custom plugin creation. Community creation via vBulletin, Ning and BuddyPress and bbpress

I also am well experienced in site conversion, transition and merges. To clarify this, website technologies change and giving up your data is not an option. I have transitioned literally hundreds of sites from one platform to another.

viagra 50 mg indian version of viagra cialis cheapest viagra india online viagra cost comparison viagra for sale without prescription generic tadalafil online buy viagra in korea indian levitra discount cialis online viagra prescription over the counter vardenafil cialis otc cialis no rx cialis 30 mg viagra ranbaxy buy levitra in uk cialis low price tadalafil tablets 10mg cheap viagra fast shipping cheap generic levitra cialis discount cialis 5mg viagra discount prices buy levitra without prescription vardenafil online generic levitra canada viagra professional price cheapest sildenafil citrate indian version of cialis viagra lowest price viagra online prescriptions tadalafil 10mg levitra over the counter levitra prescriptions online buy viagra without a prescription liquid tadalafil citrate buy viagra prescription online tadalafil 20mg india india viagra generic sildenafil citrate for sale vardenafil hcl 10mg cialis discount coupon buy levitra australia viagra over the counter in canada liquid sildenafil tadalafil price comparison viagra cost in india cialis mail order sildenafil sales buy vardenafil cialis offer cheap vardenafil generic cialis no prescription viagra tabs generic indian names viagra price canada vardenafil hcl 20 mg generic viagra without prescription viagra by scilla biotechnologies buy generic cialis free viagra viagra over the counter viagra pills kamagra 100 mg cialis from india tadalafil australia tadalafil 20mg tablets tadalafil soft tabs sildenafil pills viagra no prescription required generic viagra paypal tadalafil online indian viagra cost tadalafil online pharmacy generic soft viagra sildenafil soft tablets viagra generic names buy viagra in ireland levitra without prescription levitra online purchase cialis pill indian tadalafil levitra 5mg cialis cost per pill tadalafil oral jelly sildenafil no prescription vardenafil price generic cialis 10mg cheap cialis no prescription order sildenafil citrate indian generic viagra blue viagra buy cialis usa apcalis 20mg tablets viagra overnight delivery sildenafil india purchase viagra without a prescription viagra prescriptions order viagra without prescription viagra with no prescription levitra for sale purchase viagra canada discount levitra viagra 200mg cheap viagra 100mg cialis overnight delivery buy sildenafil online viagra made in india cialis tabs 10mg viagra indian pharmacy viagra for sale in ireland viagra uk prices buy viagra in europe generic cialis india levitra online viagra for sale india buy viagra in dublin generic cialis soft tabs viagra 50mg cost generic sildenafil 100mg tadalafil generic viagra super active 100 mg kamagra 100mg sildenafil 100 mg tablets cialis no prescription viagra low price online cialis suhagra tablets buy cialis daily use tadalafil sample cialis prices viagra prescription online buy cialis pill kamagra from india cialis online levitra mg vigora india vardenafil 10 mg sildenafil citrate 100mg buy viagra in india buy cialis professional viagra in india buy viagra in singapore generic revatio viagra substitutes sildenafil canada viagra no script cheap kamagra viagra retail price cheap lovegra order viagra uk buy cialis in mexico viagra prescription price purchase cialis online without prescription online cialis prescription ranbaxy caverta buy viagra in hong kong sildenafil price cialis mastercard buy viagra in england viagra mail order canada cialis tablets for sale order cialis cialis soft tabs generic levitra india tadalafil prices cheap sildenafil citrate tablets cialis online prescriptions cialis 5 mg daily levitra prices prescriptions viagra viagra over the counter alternative cialis 20 mg tablets cialis generic india cialis prescribing cialis 20mg daily sildenafil 50 mg viagra drug prices tadalafil generic india cialis sale viagra prices buy viagra 50 mg levitra pharmacy buy viagra generic viagra prescription drug cialis daily cost vardenafil uk viagra soft tabs online buy viagra super active cialis 10mg price 25mg viagra silagra 100mg online viagra prescriptions cialis prescription cheap cialis india revatio 20 mg indian equivalent of viagra tadalafil india viagra capsules cheapest viagra buy cialis without prescription tadalafil overnight cheap tadalafil online purchase viagra online no prescription