1

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 :)

3
  • Those techniques are redirects, not links, they are supposed to change pages based on code or time, not based on user input etc. Commented Jun 7, 2012 at 15:36
  • If you are building a website, be very careful doing so with limited PHP experience. Eventually you will want to be able to 're-use' parts of your html page, and be able to edit HTML content. In the long run, you might be better off looking into Content Management software like Wordpress, Joomla or Drupal. But I will refrain from discouraging you any further, just because PHP is a nice language to pick up. Commented Jun 7, 2012 at 15:38
  • Thanks for your comment. I feel comfortable with my skills as a programmer, just less so with PHP, once I get back in the swing of it, it should be ok... :) Commented Jun 7, 2012 at 15:42

8 Answers 8

10

Just make them regular links

<a href="contact_us.php">Contact</a> 
Sign up to request clarification or add additional context in comments.

2 Comments

A solution so simple...lol why on earth did I not think of that.....thanks! Will do (unless someone else disagrees)
I wouldn't suggest any other way.
2

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> 

Comments

2

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.

4 Comments

I am...very rusty on php/html, just getting back into it after about 2 years (obviously completely forgot about <a>).
Feel free to ask anytime! .php extension is there so you can make php calls to the same page and do bunch of other stuff too. in HTML you can not make php calls, you can have forms though that make calls to a different page, that is .php or other language that handle that
Here is a question. Is it better to echo the html: <?php echo "<a href.... />"?> or just write the html outside of the <?php ?>
if you have the chance of writing in HTML, do so. If its in a script and just one line, you're fine with doing in PHP
1

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.

Comments

0

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.

Comments

0

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.

2 Comments

just to get it clear, you would use one of the methods I mentioned above if you want to change pages WITHOUT the user pushing a button or something (like a timeout for example?)
Sending HTTP Headers changes the page location instantly. If you want to change the page after a set period of time, you need to use javascript or a meta tag. However, I can't think of any sensible reason why you would want to do the latter.
0

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> 

1 Comment

I forgot the </html>, hence the -1. My apologies.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.