Log in

View Full Version : PHP links



moose86
12-22-2011, 01:46 AM
Hi guys,

I am trying to make s nav menu with 5 buttons to go to 1 php file with all of the redirect locations in, when clicked, look for a redirect link (to the expected page) and execute that redirect.

Im quite new to php and would like to know:
1. is this possible?
2. How can it be done?

Thanks in advance :)

djr33
12-22-2011, 01:53 AM
I think it's possible. Please give a little more information about what you'd like. What exactly does the user click? How is it stored? What is the result? From what you said, it's not completely clear. Just take us through it step by step and we can help you figure out how to turn that into code.

moose86
12-22-2011, 02:12 AM
No problemo, ill try best I can,

1. user clicks a div type button (i.e: one)
2. div tells the action to go to links.php with the following code:

<a href="links.php"><div id="button">one</div></a>
3. the php file has different redirect links in as follows: (not sure how to code so this is what I mean)

<?php
/* Redirect browser */
/* Add LINK */
header("Location: one.html");

/* Redirect browser */
/* Add LINK */
header("Location: two.html");

/* Redirect browser */
/* Add LINK */
header("Location: three.html");
/* Make sure that code below does not get executed when we redirect. */
exit;
?>
4. user arives on intended page (i.e: one.html)

Hope this helps

Thanks

djr33
12-22-2011, 02:16 AM
First, why not just use real links? Is there a benefit to using links.php? Do you want to track something?

The standard way to do this (perhaps only way) is to use GET variables:
<a href="links.php?redirect=one">

Then on links.php:

<?php
if (isset($_GET['redirect'])&&$_GET['redirect']=='one') {
header("Location: one.html");
}
?>

There are many possible variations on that, but that's the general idea.


Note: it's technically invalid to use a relative URL in a header redirect. Instead, you should use the full (absolute) URL for the page, such as http://yoursite.com/one.html

moose86
12-22-2011, 02:27 AM
well as you can prob tell, i make my site using div's, this provides both nicer design and is easy to manipulate if needed but the trouble with that is that the nav bar has to be created in every page, so if (in the future) I need to change a link for one of the buttons in the nav bar, i don't have to go into every single page to change the html, instead I can just change the location in that 1 php file.

as i said i am new to php, so are there any risks or anything bad about doing it this way other then what you have already told me? (which i will take into consideration)

Thanks

djr33
12-22-2011, 03:23 AM
Ok, I think you're approaching this the wrong way, and that I misunderstood in the first place.

What you're trying to do is use PHP to share content between pages? So you want to create a shared navigation menu that is on all of your pages that you can edit only one time when you want to change it? That's definitely possible.

First, learn to use include() to bring in files using PHP:
<?php include('path/to/file.php'); ?>

In this case, you'd be bringing in "menu.php" or whatever you want to call it.

In that page, you can actually JUST have the menu in HTML form with real links, divs, or whatever you want.

But within that page you could also generate links automatically from a list. For example, create an array, loop through that array, and for each entry output a link.

But if you're sharing it across all pages, modifying a little code by hand (one time) is also pretty easy. Maybe it's not worth generating the links from a list.

Have I understood now?