Form field entry directs to diff URLs based on entry?

A

AtomicBob

I have been given an interesting task:

Make a page with a single form field, which is to take a name (first
and last), and regardless of case or whether there is a space between
the first and last name, when the user hits the submit button it takes
them to a URL that has been assigned to that name.

Example 1:

user enters "Tony Tiger" in the field and hits submit.

user is then sent to tonytiger.exampledomain.com.

Example 2:

user enters "johnnywalker" in the field and hits submit.

user is then sent to johnnywalker.exampledomain.com.

Thanks very much for any advice!
 
M

Martin Jay

In message said:
I have been given an interesting task:

Make a page with a single form field, which is to take a name (first
and last), and regardless of case or whether there is a space between
the first and last name, when the user hits the submit button it takes
them to a URL that has been assigned to that name.

I've upload something to get your started to:
<http://www.spam-free.org.uk/pages/login.zip>.

It's a bit basic, and you might want to think about what should happen
if someone enters a name that doesn't exist.

Presumably this is for a referrals type application and security isn't
an issue.
 
M

Martin Jay

What do you think it would happen?

It depends how the server is configured: visitors may get a 'server not
found' error, a 404 error, a default page, or maybe something else.

The script could be modified to deal with unexpected names, either by
looking names up in a database or checking them against a list. Or they
could be hard-coded into it. :(
 
L

Luigi Donatello Asero

Martin Jay said:
It depends how the server is configured: visitors may get a 'server not
found' error, a 404 error, a default page, or maybe something else.

The script could be modified to deal with unexpected names, either by
looking names up in a database or checking them against a list. Or they
could be hard-coded into it. :(

Do you mean that if an user should fill in a form like this
https://www.scaiecat-spa-gigi.com/sv/formularet.html
and write a name
which does not exist, this user would get a 404 error?!
 
T

Toby Inkster

AtomicBob said:
Example 1:
user enters "Tony Tiger" in the field and hits submit.
user is then sent to tonytiger.exampledomain.com.

Example 2:
user enters "johnnywalker" in the field and hits submit.
user is then sent to johnnywalker.exampledomain.com.

Unless you use some client-side scripting, a form will always submit to
just one place.

However, that one place could be a script that looks at the form
submission and sends an HTTP 301 redirect to various different places
based on the form contents.

Here's some example code:

<form action="redirect.php" method="get">
<fieldset>
<legend>enter a name</legend>
<input name="name">
<input type="submit">
</fieldset>
</form>

<?php
// This file is "redirect.php".

// What is my domain name?
$mydomain = 'example.com';

// You used 'exampledomain.com' -- you might not have known
// that there exists a domain 'example.com' which is specially
// reserved just for examples.

// What name has the user entered?
$name = $_GET['name'];

// Convert down to lower case
$name = StrToLower($name);

// Remove all characters except 0-9 and a-z
$name = pReg_Replace('/[^0-9a-z]/', '', $name);

// Generate Final URL
$url = "http://{%s}.{$mydomain}/";

// Redirect
Header("HTTP/1.1 301 Redirect");
Header("Location: {$url}");
?>
 
L

Luigi Donatello Asero

Toby Inkster said:
AtomicBob said:
Example 1:
user enters "Tony Tiger" in the field and hits submit.
user is then sent to tonytiger.exampledomain.com.

Example 2:
user enters "johnnywalker" in the field and hits submit.
user is then sent to johnnywalker.exampledomain.com.

Unless you use some client-side scripting, a form will always submit to
just one place.

However, that one place could be a script that looks at the form
submission and sends an HTTP 301 redirect to various different places
based on the form contents.

Here's some example code:

<form action="redirect.php" method="get">
<fieldset>
<legend>enter a name</legend>
<input name="name">
<input type="submit">
</fieldset>
</form>

<?php
// This file is "redirect.php".

// What is my domain name?
$mydomain = 'example.com';

// You used 'exampledomain.com' -- you might not have known
// that there exists a domain 'example.com' which is specially
// reserved just for examples.

// What name has the user entered?
$name = $_GET['name'];

// Convert down to lower case
$name = StrToLower($name);

// Remove all characters except 0-9 and a-z
$name = pReg_Replace('/[^0-9a-z]/', '', $name);

// Generate Final URL
$url = "http://{%s}.{$mydomain}/";

// Redirect
Header("HTTP/1.1 301 Redirect");
Header("Location: {$url}");
?>

How does the file redirect.php file decide to which URL it shall send the
form content?
 
L

Luigi Donatello Asero

Luigi Donatello Asero said:
Toby Inkster said:
AtomicBob said:
Example 1:
user enters "Tony Tiger" in the field and hits submit.
user is then sent to tonytiger.exampledomain.com.

Example 2:
user enters "johnnywalker" in the field and hits submit.
user is then sent to johnnywalker.exampledomain.com.

Unless you use some client-side scripting, a form will always submit to
just one place.

However, that one place could be a script that looks at the form
submission and sends an HTTP 301 redirect to various different places
based on the form contents.

Here's some example code:

<form action="redirect.php" method="get">
<fieldset>
<legend>enter a name</legend>
<input name="name">
<input type="submit">
</fieldset>
</form>

<?php
// This file is "redirect.php".

// What is my domain name?
$mydomain = 'example.com';

// You used 'exampledomain.com' -- you might not have known
// that there exists a domain 'example.com' which is specially
// reserved just for examples.

// What name has the user entered?
$name = $_GET['name'];

// Convert down to lower case
$name = StrToLower($name);

// Remove all characters except 0-9 and a-z
$name = pReg_Replace('/[^0-9a-z]/', '', $name);

// Generate Final URL
$url = "http://{%s}.{$mydomain}/";

// Redirect
Header("HTTP/1.1 301 Redirect");
Header("Location: {$url}");
?>

How does the file redirect.php file decide to which URL it shall send the
form content?


Do you mean perhaps that it will show a page with 404 error to the user if
he or she has filled in the form a name which contains forbidden
characters?
 
L

Luigi Donatello Asero

Martin Jay said:
Swedish? Unfortunately the English version of the page isn't available
yet. :(


As my one-man business is in Sweden , I usually write the pages in Swedish
first, I often write the pages in Italian ( I am Italian and I was grown up
in Italy
and in German afterwards.....English is not my first priority on the
website.. so I usually write the pages in English later
because there are many more competiting pages in the English language than
for example in Swedish or Finnish or Italian..
Why should I show the content to competitors before I display them to
customers?
(they often understand English...)

My comments were not about forms in general, but specifically about the
setup described by AtomicBob at the beginning of the thread.

Ok.
The thread is interesting anyway....

--
Luigi Donatello Asero
https://www.scaiecat-spa-gigi.com/sv/europa/italien/markerna/ancona/numana/
今天二零零六年四月二åä¹æ—¥
星期五六



https://www.scaiecat-spa-gigi.com/sv/europa/italien/markerna/ancona/numana/
 
L

Luigi Donatello Asero

Luigi Donatello Asero said:
As my one-man business is in Sweden , I usually write the pages in Swedish
first, I often write the pages in Italian ( I am Italian and I was grown up
in Italy


I grew up in Italy and I lived some years in Germany. Germany is an
important commercial partner for both Sweden and Italy.
At the same time there are less people speaking for example German, Swedish
and Italian than English...
Not every European speaks Chinese either which is one of the reasons why I
am learning Chinese....
 
J

Jonathan N. Little

Luigi said:
Luigi Donatello Asero said:
"Toby Inkster" <[email protected]> skrev i meddelandet
// Convert down to lower case
$name = StrToLower($name);

// Remove all characters except 0-9 and a-z
$name = pReg_Replace('/[^0-9a-z]/', '', $name);

Do you mean perhaps that it will show a page with 404 error to the user if
he or she has filled in the form a name which contains forbidden
characters?


Not necessarily. If you look at the function, he is stripping out all
non-alpha numeric characters, if what is left matches a page that exists
then it would be valid else you would get the 404. Of course one would
normally use the cleaned form input and look up from either a listing or
database for the file. If not found it could use a fall-back page with a
Table of Contents of valid filenames/urls.
 
D

Dustin

This seems a lot easier to me using JavaScript

<html>
<head>
<script>
function redirect()
{
var domain = "exampledomain.com";
var name = document.getElementById("name").value;
name = name.toLowerCase(); //put everthing in lower case
name = name.replace(" ", "");
document.location.href = "http://" + name + "." + domain;
}
</script>
</head>
<body>
Name: <input type="text" id="name" /><br />
<button onclick="javascript:redirect()">Submit</button>
</body>
</html>
 
T

Toby Inkster

Dustin said:
<script>
function redirect()
{
var domain = "exampledomain.com";
var name = document.getElementById("name").value;
name = name.toLowerCase(); //put everthing in lower case
name = name.replace(" ", "");
document.location.href = "http://" + name + "." + domain;
}
</script>

What if the user doesn't have Javascript enabled? Important things like
navigation shouldn't rely on client-side scripting.

What if they type in the name "John O'Groats"? Treat all user input as
hostile by default.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top