Page 1 of 1
A quick PHP question
Posted: Thu Feb 25, 2010 2:00 pm
by richrich123
Hi,
I wonder if any of you who are good with PHP can advise, I know this is basic but I am just trying to spruce up an old PHP website I have and clearly PHP is not an area i know much about
I want to be able to have a single page called links.php that contains a list of all my links used across the site to save having to add / ammend / update each link individually across the site eveytime I change one.
I can half achieve this by having a page called links.php and adding one link to it and then calling this from other pages by having the link point to href="links.php". This will then execute the single link contained in links.php
What I know you can do (but can't find how) is to have the links.php contain lots of links and be able to select which one is used. On other PHP sites I have looked at, I can see people doing this with links that look like this - href="links.php?link=5"
Clearly this is telling it to open the link contained in links.php that is numbered 5. Can anyone tell me the code used to identify each link. In plain terms I mean the code used to uniquely identify each link in links.php
If this was my link in links.php:
<a href="http://www.site2.co.uk" target="_blank">test link 2
what else do I need to add to identify it as link=5 so I can call this link from the list.
I have scanned many dev sites but just can't find it.
Cheers
Re: A quick PHP question
Posted: Thu Feb 25, 2010 2:26 pm
by lshaw
You could store your links in a database, and the ?link=5 on other sites is the id of the link from the database.
You can also have a field called `type` and set it to navbar or general so you can put all links in one table
Re: A quick PHP question
Posted: Thu Feb 25, 2010 2:35 pm
by richrich123
Thanks,
I would say I only have about 20 different links across my site so don't think I really need to involve a db.
I just want to have a links.php file like this
(link id 1) <a href="
http://www.site1.co.uk" target="_blank">test link 1
(link id 2) <a href="
http://www.site2.co.uk" target="_blank">test link 2
(link id 3) <a href="
http://www.site3.co.uk" target="_blank">test link 3
etc
etc
And on my main pages e.g index.php where I want the link to be displayed to have it
Please feel free to vistit
/links.php?link1
All I need to work out is how I link /links.php?link1 to the relevant url in the links.php page
Re: A quick PHP question
Posted: Thu Feb 25, 2010 2:42 pm
by AbraCadaver
This is most frequently in a database as lshaw has stated. links.php would lookup the link where the id=5 in the database. I'm not sure how complex your site is, and I'm assuming that you're talking about links to other sites, but a simple approach might be this:
links.php
Code: Select all
$id = $_GET['id'];
$links[1] = 'http://www.google.com';
$links[2] = 'http://www.yahoo.com';
header('Location: ' . $links[$id]);
Your links would look like: links.php?id=1
You could make it more meaningful to you like:
links.php
Code: Select all
$id = $_GET['id'];
$links['company1'] = 'http://www.google.com';
$links['company2'] = 'http://www.yahoo.com';
header('Location: ' . $links[$id]);
Your links would look like: links.php?id=company1
Re: A quick PHP question
Posted: Thu Feb 25, 2010 2:47 pm
by richrich123
AbraCadaver,
Thats excellent, that is all I need. I'll give it a try but looks exactly what I am trying to achieve.
Many thanks
Re: A quick PHP question
Posted: Thu Feb 25, 2010 2:58 pm
by AbraCadaver
richrich123 wrote:AbraCadaver,
Thats excellent, that is all I need. I'll give it a try but looks exactly what I am trying to achieve.
Many thanks
Cool. We posted our last posts at about the same time so I didn't see your other one until after. What I posted may work for you, but based on your post before that, it appears you want to get the link and the description so that you can display it in the a tag? If so, then abandon the other idea. This is more to that point:
yourpages.php
Code: Select all
include('links.php');
//some php/html whatever is there now
echo '<a href="' . $links[1]['url'] . '" target="_blank">' . $links[1]['text'] . '</a>';
links.php
Code: Select all
$links[1] = array('url' => 'http://somewhere.com', 'text' => 'Click here');
$links[2] = array('url' => 'http://somewhere2.com', 'text' => 'Click here 2');
Or this may be easier to read:
Code: Select all
$links[1]['url'] = 'http://somewhere.com';
$links[1]['text'] = 'Click here';
Or of course using something more descriptive:
Code: Select all
$links['company1']['url'] = 'http://somewhere.com';
$links['company2']['text'] = 'Click here';
Re: A quick PHP question
Posted: Thu Feb 25, 2010 3:09 pm
by richrich123
Doesn't seem to work.
I have added your code to the links.php page
Code: Select all
<html>
$id = $_GET['id'];
$links[1] = 'http://www.google.com';
$links[2] = 'http://www.yahoo.com';
header('Location: ' . $links[$id]);
</html>
Then on a blank test page added the link
Code: Select all
p>here is a <a href="links.php?id=1" target="_blank">link</a></p>
when I then click the above link, rather than open google.com, I get a blank web page open with the php line:
Code: Select all
$id = $_GET['id']; $links[1] = 'http://www.google.com'; $links[2] = 'http://www.yahoo.com'; header('Location: ' . $links[$id]);
Am I doing anything wrong?
Thanks
Re: A quick PHP question
Posted: Thu Feb 25, 2010 3:15 pm
by AbraCadaver
Well, yeah, I thought this was your PHP site and you had used PHP before, even if in a limited way
Code: Select all
<?php
$id = $_GET['id'];
$links[1] = 'http://www.google.com';
$links[2] = 'http://www.yahoo.com';
header('Location: ' . $links[$id]);
?>
Re: A quick PHP question
Posted: Thu Feb 25, 2010 3:34 pm
by richrich123
Sorry, the tags were there, I just didn't copy them in.
That works great. The one last thing that would be good (and that I was also trying to achieve)
Some of my links are affiliate ones which causes 2 issues. Firstly they can be very long and secondly it is better to conceal so they can't be hijacked. The idea of having the actual url in a links.php was also so that the user cannot identify the link. With the code you have provided, when you hover the mouse on the 'click here' link on the page, you can see the target
http://google.com in the bottom left of the browser.
Is there something that can be ammended so the link does not show. On another site I was looking at, when you hover on a link you just see
http://domain.com/links.php?link=1
Click it and it takes you to the target page. For example I have an actual link that is
http://match.com/id=99999999999999 (my affiliate link as such)
Ideally I want the links on my pages when hovered over or looked at to show
http://domain.com/links.php?link=1. On clicking, links.php?link=1 gets resolved via the links.php page that the browswe never gets to see. The user is directed to the target link at
http://match.com/id=99999999 which on arrival, match resolve the address back to
http://match.com having recorded my ID as the sender.
Hope that makes sense.
Thanks
Re: A quick PHP question
Posted: Thu Feb 25, 2010 3:51 pm
by AbraCadaver
Just use the first way I posted, or this would be a combination of the two approaches:
yourpages.php
Code: Select all
include('links.php');
//some php/html whatever is there now
echo '<a href="links.php?id=1" target="_blank">' . $links[1]['text'] . '</a>';
links.php
Code: Select all
$links[1] = array('url' => 'http://somewhere.com', 'text' => 'Click here');
$links[2] = array('url' => 'http://somewhere2.com', 'text' => 'Click here 2');
if(isset($_GET['id'])) {
$id = $_GET['id'];
header('Location: ' . $links[$id]['url']);
}
Re: A quick PHP question
Posted: Thu Feb 25, 2010 3:59 pm
by VladSun
Always use exit() after a header('Location: xxxxxxx')
Re: A quick PHP question
Posted: Thu Feb 25, 2010 4:27 pm
by AbraCadaver
VladSun wrote:Always use exit() after a header('Location: xxxxxxx')
As a best practice, yes.
Re: A quick PHP question
Posted: Thu Feb 25, 2010 5:18 pm
by richrich123
AbraCadaver,
Many thanks for all your help, all working as I had wanted. Much appreciated mate.
VladSun, as you may have worked out, PHP is not my thing,
Always use exit() after a header('Location: xxxxxxx')
Code: Select all
header('Location: ' . $links[$id]['url']);
}
exit()
?? Not knowing PHP, I not sure how you apply this, is it as simple as adding at the end of the code? You mention this is best practice, i'll happily add it, what does it achieve?
Thanks again for all your help. This was just an old site someone designed for me a while back that i just wanted to make a few changes to, you help is much appreciated.
Cheers
Rich
Re: A quick PHP question
Posted: Thu Feb 25, 2010 7:36 pm
by AbraCadaver
Put it directly after the header() line.