/    Sign up×
Community /Pin to ProfileBookmark

Re-Post: Help Creating Custom Hyperlinks

Hello,

This is a re-post of a question I posted today in the JavaScript forum. I was told that what I am looking for needs to be run serverside, so PHP is the answer. I have never used PHP before, but am hoping that my situation will be easy to solve.

I searched this forum and found several posts that are similar to mine ([B][I]Unique User URLs, Major help needed please, form-website database, $_GET and $variables[/I][/B]), but different enough that they do not answer my question.

Here is what I am trying to do. I have a website created in FrontPage 2002. The website is running on an Apache server and I have access to a MySQL database. I want to have code running on a page (called pagename below) that displays a “semi-custom” hyperlink for each visitor. The hyperlink will take visitors to a .NET application that I have running on a different server.

Ideally, when someone first opens “pagename” the link would be displayed on the page in a place I specify. If need be, I could have a button that the user has to click to generate the link.

This is what I mean by “semi-custom” link. The link will look like this:

http://website.com/pagename/?=[B][COLOR=Blue]XXXXX[/COLOR][/B]&m=0″

The XXXXX is the only custom part. The text before the XXXXX and the &m=0 is the same for everyone. The XXXXX is a unique userid that my .NET application will recognize. The userid is typically alphanumeric. However, I thought I could simplify this task if I made it a simple integer that starts at the number 1 and increases by one for each new visitor. This way I am hoping that PHP can generate an infinite number of links.

#1 sees “http://website.com/pagename/?=1&m=0
#2 sees “http://website.com/pagename/?=2&m=0
#3 sees “http://website.com/pagename/?=3&m=0

Can anyone tell me what the PHP code would be to display these links? Also, I am hoping that once I have the code all I need to do is insert it into the body of my webpage. If that is incorrect, please advise.

Thank you in advance for your help! ?

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@GenixdeaeJul 24.2006 — You're wanting to do that part w/o a database correct?

One solution would be to make a table in your database called 'unique_visitors'(or something like that) then everytime someone comes to your site, have a insert statement that inserts a new record, then redirect to pagename and grab the unique id that the db generated for the redirect...that's one solution, prolly the ugliest, but it'd work

Another solution would be to get/make a script that counts each unique visitor, then modify the script so that it will return XXXXX in you're url for a redirect to pagename, this is pretty much the samething as above, but using sessions/cookies instead of a Database

A third solution would be to make a if statement that checks the users IP address, and if it's differant then add 1 to the previous users number...Example[code=php]if($_SERVER['REMOTE_ADDR'] != $previous_ip) { //check to see if the new user is a diff ip then the previous
$number = number + 1;
$previous_ip = $_SERVER['REMOTE_ADDR'];
//redirect to http://website.com/pagename/?=$number&m=0
}[/code]

hope that helps ya some
Copy linkTweet thisAlerts:
@Mr__ChipauthorJul 24.2006 — Genixdeae,

You are absolutely correct that a database solution makes the most sense. The problem is that my only experience with databases is with MS Access and MS SQL Server. My website is hosted on a LINUX server that is running MySQL. I know nothing (aka Sgt. Shultz) about how to set up a database and the queries on this platform. Over time I would like to learn some basics. For now, I am trying to find the easiest solution to this one need. This is why I am shying away from your first option. My ISP showed me how to create a database and table in MySQL. If you could tell me what the query is and where I would put it in my page, we could try this.

I have a few questions about your second option:

[COLOR=Blue][I]Another solution would be to get/make a script that counts each unique visitor, then modify the script so that it will return XXXXX in you're url for a redirect to pagename, this is pretty much the samething as above, but using sessions/cookies instead of a Database

[/I]
[/COLOR]


1) What do you mean by unique visitor. If John Doe in Cleveland visits on Monday and returns on Tuesday, would he see two links? This may be OK because I may require people to enter an email address that is set up as a primary key in the database.

2) What happens if someone has cookies blocked. Would they see nothing?

I tried to implement your third option, but it did not work. This is likely due to my lack of knowledge here. This is what I did.

1) I copied your code into a text editor and enclosed it in the PHP tags of "<?php" at the start and "?>" at the end.

2) I pasted it into my web pages html code in the body.

When I view that page nothing happens. :o

Is there something I am supposed to do to activate PHP on my website?

Was I supposed to change your text in any way besides the redirect address?

Are the // in "//redirect to http:..." correct? I thought // commented out the line. But I may be off.

Thanks so much for your help!
Copy linkTweet thisAlerts:
@GenixdeaeJul 24.2006 — The first option: too late to get into details right now, and i'm sure by the time i'm awake, you'll have a solution

the second option:

1)Yes he'd see two differant links each time he visited

2)correct, the redirect would be without a number .... /?=&m=0....with my understanding

the third option:

1)as long as your host supports PHP, no nothing u need to do

2)you should have gotten a few errors :o , wasn't very good coding on my end

3)the // does comment out code, i did that because i can't remember the code at the moment to do a redirect....

CHanges to the code:[code=php]if(!isset($previous_ip)) {
$number = "1"; //declare number for the first user to be 1
$previous_ip = $_SERVER['REMOTE_ADDR']; //declare it for the very first user or incase the server resets
echo "<meta http-equiv="refresh" content="1;URL=http://website.com/pagename/?=<?PHP $number ?>&m=0">"; //it will redirect to the site after 1 second
}elseif($_SERVER['REMOTE_ADDR'] != $previous_ip) { //check to see if the new user is a diff ip then the previous
$number = $number + 1; //Add 1 to the previous viewers number
$previous_ip = $_SERVER['REMOTE_ADDR']; //redeclare the variable so we can determain unique visitors
echo "<meta http-equiv="refresh" content="1;URL=http://website.com/pagename/?=<?PHP $number ?>&m=0">"; //it will redirect to the site after 1 second
}//end our if[/code]
Copy linkTweet thisAlerts:
@Mr__ChipauthorJul 24.2006 — [B]Wow, this is so nice of you to help me out this way[/B]. I am on the West Coast - not sure what time it is for you but it is getting late. I put your new code inside the <?php and ?> tags. When I go to the page, nothing happens. But now I see the part of your code colored blue displayed on the screen.


if(!isset($previous_ip)) {

$number = "1"; //declare number for the first user to be 1

$previous_ip = $_SERVER['REMOTE_ADDR']; //declare it for the very first user or incase the server resets

echo "<meta http-equiv="refresh" content="1;URL=URL=http://website.com/pagename/?=<?PHP $number ?>[COLOR=Blue]&m=0">"; //it will redirect to the site after 1 second

}elseif($
_
SERVER['REMOTE_ADDR'] != $previous_ip) { //check to see if the new user is a diff ip then the previous

$number = $number + 1; //Add 1 to the previous viewers number

$previous_ip = $_SERVER['REMOTE_ADDR']; //redeclare the variable so we can determain unique visitors

echo "<meta http-equiv="refresh" content="1;URL=http://website.com/pagename/?=<?PHP $number ?>&m=0">"; //it will redirect to the site after 1 second

}//end our if[/COLOR]


I think we are getting closer. If you don't mind, would you take a look at this tomorrow when you are better rested?

Also, this may be a silly question, but should my webpage with this code end in .htm, .html, or .php?

I will check with my ISP to make sure that PHP is enabled on my account.

Thanks and good night!!
Copy linkTweet thisAlerts:
@Mr__ChipauthorJul 24.2006 — I just checked and my ISP has the following installed on my server:

mysql Ver 12.22 Distrib 4.0.16

perl version 5.8.3

PHP 4.4.1

My ISP described a simple test to make sure PHP is running on my server - and it is. In that test they said that the file needs to end with the .php extension, that it needs to either be in my root directory or in my public_html folder. When I moved it to my root directory I had my first real error message:

[COLOR=Red][B]Parse error: parse error, unexpected '}', expecting ',' or ';' in /home/website/public_html/test2.php on line 241[/B][/COLOR]
Copy linkTweet thisAlerts:
@GenixdeaeJul 24.2006 — Can u put on here what line 239 to 243 contain?
Copy linkTweet thisAlerts:
@Mr__ChipauthorJul 24.2006 — Hi,

Here are lines 229 through 247 (in case the extra lines help)

229: $previous_ip = $_SERVER['REMOTE_ADDR'];

230: //redirect to http://website.com/pagename/?=$number&m=0

231: }

232:

233: ?>

234:

235:

236: <?php

237: if(!isset($previous_ip)) {

238: $number = "1"; //declare number for the first user to be 1

239: $previous_ip = $_
SERVER['REMOTE_ADDR']; //declare it for the very first user or incase the server resets

240: echo "<meta http-equiv="refresh" content="1;URL=http://website.com/pagename/?=<?PHP $number ?>&m=0">" //it will redirect to the site after 1 second

241: }elseif($_SERVER['REMOTE_ADDR'] != $previous_ip) { //check to see if the new user is a diff ip then the previous

242: $number = $number + 1; //Add 1 to the previous viewers number

243: $previous_ip = $_
SERVER['REMOTE_ADDR']; //redeclare the variable so we can determain unique visitors

244: echo "<meta http-equiv="refresh" content="1;URL=http://website.com/pagename/?=<?PHP $number ?>&m=0">"; //it will redirect to the site after 1 second

245: }//end our if

246:

247: ?>

FYI, I will be offline from now until about 10:00 am Pacific Time.
Copy linkTweet thisAlerts:
@GenixdeaeJul 25.2006 — on line 240 there should be a ; at the end of it, must have gotten cut off when i copied/pasted
Copy linkTweet thisAlerts:
@Mr__ChipauthorJul 25.2006 — Genixdeae,

I'm sorry that I took so long to reply. I have been reading up on PHP so that I may better understand all of your good advice. We are getting close, but I think there is a subtle logic error in the code. [B]Please forgive this long and detailed post![/B]

First the good news. ? I added the ; at line 240 and now the code successfully redirects me to the other site. However, I think there is a problem in the logic in how number is calculated. I tested out the code from two different computers on two different ISPs and was directed to the same site.

To verify this problem I added some code to yours for error checking. First I extended the delay from 1 to 5 seconds so I can see what is happening. Then I added code to display the value of $number and the user's IP address. The error checking code showed that the value of $number stayed at 1 even though my two tests came from different IP addresses.

Note: In order for me to be able to echo the IP address, I had to remove the single quotes from around REMOTE_ADDR as highlighted here: $_SERVER[[B][COLOR=Red]'[/COLOR][/B]REMOTE_ADDR[B][COLOR=Red]'[/COLOR][/B]] while yours have REMOTE_ADDR enclosed in single quotes. I tried removing the single quotes from your code and had the same problem.

Following is the latest version of your complete code. Any ideas why $number is not working?

[code=php]
<html>
<head>
</head>
<body>
<b>Genixdeae code idea that automatically forwards the user, set for 5 second delay
<br><br>
You will be automatically forwarded to the new site in 5 seconds.</b>
<?php
if(!isset($previous_ip))
{
$number = "1";
//declare number for the first user to be 1
$previous_ip = $_SERVER['REMOTE_ADDR'];
//declare it for the very first user or incase the server resets
/*xxx Mr. Chip Question. Does this mean anytime the server reboots the counting will start over again at 1? If that is the case I can I simply change this code so $number is set to say 295 to start off where it left off before the reboot? This is where a database would probably come in handy.xxx*/
echo "<meta http-equiv="refresh" content="5;URL=http://website.com/pagename/?=<?PHP $number ?>&m=0">";
//it will redirect to the site after 5 seconds
}
elseif($_SERVER['REMOTE_ADDR'] != $previous_ip)
{
//check to see if the new user is a different ip then the previous user
$number = $number + 1;
//Add 1 to the previous viewers number
$previous_ip = $_SERVER['REMOTE_ADDR'];
//redeclare the variable so we can determain unique visitors
echo "<meta http-equiv="refresh" content="5;URL=http://website.com/pagename/?=<?PHP $number ?>&m=0">";
//it will redirect to the site after 5 second
}
//end our if
?>
<br><br>
<b>
<?php
echo "the calculated number for quality checking is $number";
?>
<br>
<?php
echo "my IP address is $_SERVER[REMOTE_ADDR]";
?>

</b>
</body>

[/code]
Copy linkTweet thisAlerts:
@Mr__ChipauthorJul 26.2006 — I did some more testing on the code and learned the following:

1) $previous_ip is not working. All it does is show the current user's ip. When I click on the page from a different ip address, [B]both[/B] $_SERVER[REMOTE_ADDR] and $previous_ip change to reflect the new ip.

Then I found something on another site that makes me question the idea of using $_SERVER[REMOTE_ADDR]. People were commenting that users IP will be affected depending if they are behind a firewall or using NAT. I am not familiar with $_SERVER[REMOTE_ADDR]. It is a reliable way of making sure only new visitors get new links?

This other site posted the following code to get someone's true IP address. Any comments if I should use this code or stick with just $_SERVER[REMOTE_ADDR]?

[code=php]
function getIP() {
$ip;

if (getenv("HTTP_CLIENT_IP")) $ip = getenv("HTTP_CLIENT_IP");
else if(getenv("HTTP_X_FORWARDED_FOR")) $ip = getenv("HTTP_X_FORWARDED_FOR");
else if(getenv("REMOTE_ADDR")) $ip = getenv("REMOTE_ADDR");
else $ip = "UNKNOWN";

return $ip;

}
[/code]
Copy linkTweet thisAlerts:
@GenixdeaeJul 27.2006 — hey man, i'll get back to ya as soon as i can, I've been working 12 hour shifts for the past few days, so i get home and go to sleep, i havn't forgotten bout ya ?
Copy linkTweet thisAlerts:
@Mr__ChipauthorJul 27.2006 — Genixdeae,

Thank you again. I realize this is a volunteer effort on your part and [U]completely understand[/U] that this is not your first priority. I have had to deal with many 12-hour shifts myself.

[B]Please don't be mad :o[/B] , but I am starting to think that a variation of your original suggestion [B][I][COLOR=Blue](You're wanting to do that part w/o a database correct? One solution would be to make a table in your database called 'unique_visitors' then everytime someone comes to your site...)[/COLOR][/I][/B] may be a better strategy. There are two [U]potential[/U] flaws to the previous_ip strategy we have been developing:

1) Will this method work under all the firewall, router, NAT scenarios, and

2) What happens in User #1 visits the site Monday morning, then comes back again later Monday? Since other people visited it in between, his ip will not match the previous_ip and he will be given a new link.

For these reasons, I am starting to think that I would be much better off using a database. And instead of IP address, I should use their email address as a primary key. Here is my thinking:

1) Create a MySQL database table with 5,000 records. The table has fields for email address, hyperlink, and "used".

2) Set email address as a primary key to prevent duplicate email addresses. Fill the 5,000 records with simple numbers, 1 through 5,000 as unique filler values.

3) "Used" is a boolean variable with a default value of No.

4) When someone comes to my web page, they are asked to enter their complete email address into a field. It would be great if PHP could check that the email address is valid.

5) A query is then run that checks the database to first make sure that the email address is not a duplicate. If that email address was previously entered, they get a message that says they already answered the questions. If it is a new email address, the query looks for the first record that has not been used (Used = No). It then displays the hyperlink for that one record. Finally, it changes the value of "Used" in that record from No to Yes so that hyperlink is never shown again.

This sounds a lot more complicated. But I think it will work out better. Collecting email addresses is not a problem because visitors will be providing that to me anyways. The challenge is that I have no experience setting up a MySQL database and no knowledge of how to write the query and design the page. (I do know how to do this in SQL Server and Access). I have started the process of setting up the database.

What do you think of switching to your orginal approach? Would you be willing to help me implement it? ?

Thanks so much for your time!

Mr. Chip
Copy linkTweet thisAlerts:
@GenixdeaeJul 27.2006 — no worries mate....

what i would do instead of having them re-enter their email(as they could very easly put in a second email) i'd create a cookie on the users machine that contains their link ID number....less people are likly to go editing cookies then put in a secondary email

CREATE TABLE table_name (
<span><code>email_addy</code></span> varchar(100),
<span><code>link_id</code></span> int(5) auto_increment,
<span><code>used</code></span> boolean default no,
PRIMARY KEY <span><code>email_addy</code></span>);
if you run the above code through PHPMyAdmin(which your host should have) it will take care of it for you or give any errors if there are any
[code=php]//This is a function i found off google, it checks the email to make sure it's in the valid format([email protected]),
// it doesn't check to make sure it's a real email(so [email protected] is accepted)
function isvalid_email($email) {
$regexp = "^([_a-z0-9-]+)(.[_a-z0-9-]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*(.[a-z]{2,4})$"; //Validate the email address
$valid = 1; //Presume it is valid
if(eregi($regexp, $email)) {
return $valid;
}
}

//to check you'd use the code
if(isvalid_email([email protected])) {
echo "true";
}else{
echo "The email address entered is not the correct format. Please go back and try again.";
}[/code]
×

Success!

Help @Mr__Chip spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...