Problem with PHP code that I can not figure out


#1

I am new at PHP and I am in charge of up moving and updating our web site. I can across some PHP that is now working right and would appreciate any help. The first piece of coding is suppose to load the name of PHP file into the variable $page. The next piece of coding is suppose to open that file to load the screen that was selected.
[php]<?php
$page = $_GET[‘page’];
$contactName = $_GET[‘contactName’];
$contactName = str_replace("-"," ",$contactName);

if ($page == “”) {
$page = “index”;
}
else if ($page == “contact”) {
$hideContact = true;
}

//Determines which link is active and apply an appropriate class identifier
function chkPage($whichPage) {
global $page;
global $linkActive;
if ($whichPage == $page) {
if ($whichPage == “employees”||$whichPage == “legal”){
return “active”;
}
else
{
return " class=“active”";
}
}
}

$topLinks = “<li” . chkPage("") . “><a href=”/">Home Page\n";
$topLinks .= “<li” . chkPage(“pipeline”) . “><a href=”/pipeline/">Pipeline Services\n";
$topLinks .= “<li” . chkPage(“oilfield”) . “><a href=”/oilfield/">Oilfield Services\n";
$topLinks .= “<li” . chkPage(“safety”) . “><a href=”/safety/">Safety & HR\n";
$topLinks .= “<li” . chkPage(“sales”) . “><a href=”/sales/">Sales & Marketing\n";
$topLinks .= “<li” . chkPage(“contact”) . “><a href=”/contact/">Contact Us\n";
$topLinks .= “<li class=“last " . chkPage(“employees”) . “”><a href=”/employees/”>Employees Area\n";[/php]

Main screen for web site

[php]

Welcome to Duphil, Inc. <?php include("includes/vars.php"); ?> <?php include("includes/top.php"); include("includes/pages/" . $page . ".php"); include("includes/bottom.php"); ?> [/php]

#2

Well, I think this part is incorrect:

<?php include("includes/top.php"); include("includes/pages/" . $page . ".php"); include("includes/bottom.php"); ?>

Should be something like this:
[php]

<?php include("includes/top.php"); include("includes/pages/" . $page . ".php"); include("includes/bottom.php"); ?>

[/php]
NOTE: The color codes that were stuck inside the PHP script were for use inside of a “template”.
(It was not for use as true PHP code!) Hope that makes sense and helps! Good luck…


#3

I tried the changes and it did not work. The first page displays properly but when I click on one of the links it gives me a “Web page cannot be found” error. Which seems like it is not returning the correct $page value from the first PHP file. Is there a way to display that value on the screen so I can check the value or does anyone else have any ideas on how to correct this problem. Thanks for any help.


#4

Well, my guess is that the data you are placing inside the variable which gets posted is not correct.

Your code is using values like: “contact”, “index”, etc… In the page that has the links inside of the HREF’s, you use something like: <li" . chkPage(“pipeline”) . “><a href=”/pipeline/">Pipeline <span…
So, normally that would be something like:

  • <a href="pipleline.php/>Pipeline<span…

    I think it is not turning into a valid HREF… So, a trick-of-the-trade…
    Bring up the page… (Do not click on the

  • links…)
    RIGHT-CLICK on a blank area of the page and select VIEW-SOURCE option.
    This will open up another window with the source code for the “RENDERED” page. What that means, is that it will show the entire page AFTER the PHP on the server has completed. You will see the “results” of the PHP code and should be able to easily see where the HREF is badly coded…
    I am leaving for a few hours… So, I will check in later when I get back to see if you solved it… Good luck!

  • #5

    When I click on a link and it displays the error page the view source does not display the value of $page. I think this may be some old coding that is trying to run on a new version of PHP because the web site is working okay on our old server but not on the new server we will be switching to. I am fixing to leave for the day but will be back in the morning to see if you have any more sugguestions. Thanks.


    #6

    Well, you didn’t follow my steps. VIEW-SOURCE on the page BEFORE you select the link which is bad.
    Look at the link that sends you to the bad page. Then, you will see why it is not sending you to the correct page.
    It is the layout of the HREF or link that is bad. Clicking that link will just take you to a bad page and the source of that just says 404-bad page… Look at the source where you click on the link!


    #7

    I am sorry I misunderstood what you were saying. Here is piece of the view-source that I am having problems with:
    [code

  • Pipeline Services
  • Oilfield Services
  • Safety & HR
  • Sales & Marketing
  • Contact Us
  • ][/code] And at another spot it is: [code]

    Pipeline Services

    • Mainline pipeline installation
    • Pump & compressor stations
    • Lowering & relocations
    • Pressure Testing & repair
    • More ...

    Oilfield Services

    • Site preparation for drilling purposes
    • Site restoration
    • Reserve pit & ring levee construction
    • Board road installation
    • General Services
    • More ...
    [/code] And another spot is : [code]
  • Home Page
  • Pipeline Services
  • Oilfield Services
  • Safety & HR
  • Sales & Marketing
  • Contact Us
  • Employees Area
  • [/code] I am not real sure which spot is causing the problem. If I remember right there is a way to put this in debug to find the problem. If there is and you can tell me how I try to debug this. Again Thanks.

    #8

    Hmmm, I wrote a very long note to you and in the middle reviewed what you have shown for code.
    Then, I realized you have a long list of includes and code that I can not follow without more info.

    Since you did not show code with the filenames of the code, it is hard to follow what you are trying to do.
    The first post is php code and the second was your “main web page”. It appears that these should be combined. The php code creates a $page variable received from some input not shown. This is used to create some “top-links” which are set up with invalid HREF’s. They go nowhere as they do not point to pages as they should.

    You need to explain what are the filenames of the code you showed. How does the first bit of code where the $page=$_GET[‘page’]; come from? Lastly, your first post says you need help with code NOW working… Did you mean NOT working? If so, I think it is just the way the code is laid out and the HREF’s. These can be changed easily. Give us a little more general info. I am around all day, so should be able to help you fix this up today!


    #9

    We have a web page “Test.Duphil.com” that when it loads the first piece of coding that has a very lines of coding in it is called. It calles the PHP program with the get $page variable in it and the first time around the that variable is blank, see line 6 and 7, so the web page displays okay. When I click on one of the links at the top of the web page it is trying to say go to what ever PHP program of that name I just selected. So apparently the name of the page is not getting pulled in correctly. I hope this all you need, if not let me know and I really appreciate all the help.


    #10

    Okay, that did help! I look at the real code that is created on your site. (By the way, looks very nice!)

    All of your links are in the format of … Let’s explain that to you…

    The page inside a HREF is usually an actual web page such as www.somewhere.com/somefolder/somepage.html
    or somepage.html or even somefolder/somepage.php etc… ALL of your HREF’s are /somename/ and that is all that is used inside your HREF’s.

    So, how does the browser make sense of that. It basically uses that as a base and tries to load the index file for that folder. Let’s just talk about the one called pipeline. We need to pick one to explain!
    So:

  • Pipeline Services
  • or HREF="/pipeline/
    And, we will use the test address you posted. “Test.Duphil.com
    Now, a browser sees that as a link/HREF to “www.Duphil.com/pipeline/index.html” because you did not tell
    it to use a file, only a folder name. The first slash tells it to start at the base domain name of Duphil.com. (The root of your site!) The second slash after a name means that the preceding name is a folder. So, then, it points at “www.Duphil.com/pipeline/”. Lastly, since you did not use a filename, it takes the default which on most servers is “default.html”. So, it tries to get the webpage “Test.Duphil.com/pipeline/index.html”.
    Now, hopefully, you understand that. Now, if you are taking over this site from someone else and the site was working at one point, what has changed? If you are creating the site from scratch, you must either change the links for every page to point at the correct folder and file or move the files into folders and name them index.html. (I would not do it that way as it is confusing.)
    As least now you understand why it is not working. My suggestion is to review the entire layout of the website. Look at the folder set up and where all of the files are stored. Then, alter the program to fit the layout correctly. If you need help with this, either post the layout of the site here or if private, you can post it to me in a private-message. (Never post confidential info on an open post.) Hope this all helps!

    #11

    That does make sense because the website I got the coding from was in the root directory under 3 other folders. So some of the coding is probally looking for //folder/folder2/folder3. And for us to get this website to work on our new site we had to create a sub domain. So I will have to do some more digging and see what I can find. If I cannot find anything I will let you know. Thanks.


    #12

    No problem! Glad I could make sense of it for you.

    I would suggest that you start by finding all of the pages for the site. Such as the pipeline.php or pipeline.html or whatever. (For every file) Next, make a list of them with the locations such as duphil.com/folder1/folder2/etc
    This will be needed later on when you document the site. All profession programs document their sites and include a detailed list of every file with what each does and what other files they call. Some add nice flow charts to show the flow of the site. The only reason that I am mentioning this is because it is very helpful to someone trying to take over the work. (So, it will only help you, even though a lot of detective work!) And, looking for each file and what others it calls, you will learn a lot about the entire site’s code. All good for you!

    Sorry, just wanted to give a little more insite into your project. Good luck again…


    #13

    I just wished the other developer documented this site. It sure would of been a lot less work. So I guess I will get spend the rest of the short afternoon doing just what you said. Again thanks.


    #14

    Well I have searched, changed and tested everything I could look at. I still cannot find out what the problem is. I was a main frame programmer for 26 years and I could always find a problem in a program by putting it in debug or by displaying some test screens to display my data. Is there a way to put a web page in debug or to insert some kind of temporary message with a variable name it so that I could see what the &page variable? Thanks.


    #15

    Yes, it is very easy to debug PHP code. For example, just before this line:
    include(“includes/pages/” . $page . “.php”);

    You can add something like this:
    die ($page);

    What this line does is the same as this:
    echo $page;
    die();

    In other words, it will display your variable $page and then dies.
    This will display the variable and stop processing. So, you just have to look at the bottom of the display
    and see what your data is. Remember that your line above ( include(“includes/pages/” . $page . “.php”); )
    actually includes a file from the folder ‘pages’. So, if the value of $page is, let’s say “Ernie”… Your code
    would include this file pages/Ernie.php … So, inside the folder ‘pages’ would have to be Ernie.php…
    Hope that helps…


    #16

    Thanks for the debug information but it nevers gets back to my index.php. I am using the pipeline link as a example. If I manually type in where it is suppose to go in the address bar the pipline.php file comes up. So I am thinking that it has to be something in the way the link is set up in the vars.php in the includes folder. For futher clarity this is what I tried. I started our webpage and then went and put the die command in the index.php, saved it and pressed the pipeline link. All I get everytime is the HTTP 404 error code page. Any other suggestions would greatly be appreciated.


    #17

    Well… I figured out your issue. And, it is NOT what we have been looking at.

    It appears that your original site does some “URL-rewriting” on the server-side.
    This, means that it basically hides the actual URL. This means that the yoursite.com/pipeline/pipeline.php or
    yoursite.com/pipeline/index.php just becomes only yoursite.com/pipeline/

    So, how this is done is inside a file called “.htaccess” on the server in the root of the site.
    It is a special file, has no extension (actually entire name is the extension) and should be handled
    with care. You need to get the original .htaccess file and compare it to the one that is on the new test site.
    I think you will see the changes easily.

    Do not mistype or miss-copy the lines in it. They are tricky!

    Let us know if that solves it…


    #18

    That might be part of the problem because now when I put in the .htaccess and try to start my web site I get a 500 Internal Server error. The one I FTP’d from the old web site looks exactly like the one I FTP’d to the new web site. So where would I go to learn how to update this file? Thanks


    #19

    Well, .htaccess files are tricky. Did you keep a copy of the original one on the new site?
    Also, if you want to post your current one we can look at it.

    Here is a link to a quick run down on what that file does. Might help, might make it worst!
    [php]http://corz.org/serv/tricks/htaccess.php[/php]


    #20

    The one on the orginal site I could not see by accessing their web site until I started Filezilla and connected to the old server site. So what I have listed below is what is on my computer and on the web site.

    [code]Options +FollowSymLinks
    RewriteEngine off
    RewriteBase /

    ErrorDocument 404 /index.php?page=404
    ErrorDocument 403 /index.php?page=403

    php_flag display_errors on
    php_value error_reporting 7

    RewriteRule ^$ /index.php
    RewriteRule ^about/$ /index.php?page=about
    RewriteRule ^oilfield/$ /index.php?page=oilfield
    RewriteRule ^pipeline/$ /index.php?page=pipeline
    RewriteRule ^safety/$ /index.php?page=safety
    RewriteRule ^sales/$ /index.php?page=sales
    RewriteRule ^contact/$ /index.php?page=contact
    RewriteRule ^stateLicensures/$ /index.php?page=stateLicensures
    RewriteRule ^keyPersonnel/$ /index.php?page=keyPersonnel
    RewriteRule ^projectListings/$ /index.php?page=projectListings
    RewriteRule ^legal/$ /index.php?page=legal
    RewriteRule ^techDivision/$ /index.php?page=techDivision

    RewriteRule ^equipment/$ /index.php?page=equipment
    RewriteRule ^projectImages/$ /index.php?page=projectImages
    RewriteRule ^contact/(.*)/$ /index.php?page=contact&contactName=$1
    [/code]
    Thanks.