Toadums
Toadums

Reputation: 2812

Navigating pages in php

So I have been reading around on google, and it is just a mess of different answers, with not much explanation.

My question is how do you switch between PHP pages? Suppose I have a directory on my server that has the following files in it:

index.php
about_us.php
contact_us.php

Lets suppose on all 3 pages, I have a header with 3 links:

Home
Info
Contact

What should happen when one of the buttons (lets say Contact) is hit?

I have read about 3 techniques:

Php: header("contact_us.php")
javascript: window.location = "contact_us.php";
html: <meta http-equiv="Refresh" content="5; URL="contact_us.php">

Are any of these preferred by today's standards? I read somewhere that you aren't supposed to use php's header() function now.

Any insight would be very great to help me make my decision :)

Upvotes: 1

Views: 22430

Answers (8)

Gudamore
Gudamore

Reputation: 1

I always use header("contact_us.php");. But you could do echo "<a href="contact_us.php">Contact</a>"; and just make that the link. Anytime I add a php link that is how I do it

Upvotes: 0

Kenny Cason
Kenny Cason

Reputation: 12328

I have just the solution :)

<html>
<body>
<button onclick="confirmNav() ? (doubleConfirmNav() ? navigate() : cancelled() ): cancelled();">Contact Us</button>


<script type="text/javascript">

function confirmNav() {
    var r=confirm("Do you really want to navigate to 'Contact Us'?");
    if (r==true) {
      return true;
    } else {
      return false;
    }
}

function doubleConfirmNav() {
    var r=confirm("Are you 100% sure?");
    if (r==true) {
      return true;
    } else {
      return false;
    }
}

function cancelled() {
    alert("cancelling navigation");   
}

function navigate() {
    // purposely delay the redirect to give the image of a high traffic site
     setTimeout(function() {   
         window.location = "contact_us.php";
     }, 5000);   
 }

</script>

</body>
</html>

Upvotes: 0

Funk Forty Niner
Funk Forty Niner

Reputation: 74217

You can also use this technique: (doesn't require a database)

Say you have an index.php file:

<?php
$mypage = $_GET['mypage'];
switch($mypage)
{
case "one":
    @include("one.php");
    break;

case "two":
    @include("two.php");
    break;

default:
    @include("default.php");
}
?>

Which is then referenced like this:

<a href="index.php?mypage=one">one</a>

And:

<a href="index.php?mypage=two">two</a>

etc.

A straight call to index.php would bring you to the default.php page content.

Upvotes: 1

Cameron Martin
Cameron Martin

Reputation: 6012

As everyone else has said, you just need to use regular html to make links.

You're referring to redirection methods, which changes the current location without user interaction. If you want to do this, sending a HTTP header using PHP's header() is definitely the preferred method.

Upvotes: 0

Nicol&#225;s Torres
Nicol&#225;s Torres

Reputation: 1345

You should direct call the script, or have a handler that calls it (in case you want nice urls).

<a href="/contact_us.php">Contact</a>

You shouldn't use any kind of redirect, it makes a bad impact on SEO.

Upvotes: 0

Grigor
Grigor

Reputation: 4049

You just make them links like

<a href="contact_us.php">Contact Us</a>

and whenever the link is clicked they will be taken to that page. If you are new to PHP: You can write HTML in PHP.

Upvotes: 2

Ben Ashton
Ben Ashton

Reputation: 1405

Just use the html hyper reference...

<a href="index.php">Home</a> <a href="contact_us.php">Contact Us</a> <a href="about_us.php">About Us</a>

Upvotes: 2

Scott Saunders
Scott Saunders

Reputation: 30394

Just make them regular links

<a href="contact_us.php">Contact</a>

Upvotes: 10

Related Questions