duanbohan2015 2011-06-16 05:44
浏览 81
已采纳

需要帮助获取用户提交的值并从PHP表单生成唯一的URL

In the script below, you will see a value that is submitted in the form titled "shorturl." Ultimately I would like to take that value and use it to generate a unique URL that displays all of the submitted data from the form.

Here is the form where a user will submits the data:

    <html>
    <body>

    <p>Required fields are <b>bold</b></p>

    <form action="contact.php" method="post">
<p><b>Author's Name:</b> <input type="text" name="author" /><br />
<p>Company Name: <input type="text" name="company" /><br />
<p>Address:<br /><textarea name="address" rows="5" cols="40"></textarea></p>
<p>Phone Number: <input type="text" name="phone" /><br />
<b>Title:</b> <input type="text" name="title" /><br />
<p><b>One Word Description:</b> <input type="text" name="shorturl" /><br />
<p><b>Full Description:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>

<p><input type="submit" value="submit"></p>

<p> </p>

</form>

</body>
</html>

The next bit of code is the contact.php page that will output the user data:

<?php


/* Check all form inputs using check_input function */
$author = check_input($_POST['author'], "Enter your name");
$company = check_input($_POST['company']);
$address = check_input($_POST['address']);
$phone = check_input($_POST['phone']);
$shorturl = check_input($_POST['shorturl'], "Provide a single word description");
$title  = check_input($_POST['title'], "Write a title");
$comments = check_input($_POST['comments'], "Provide a full description");




/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
    show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<body>

<b>Please correct the following error:</b><br />
<?php echo $myError; ?>

</body>
</html>
<?php
exit();
}
?>

<head>
    <title><?php echo $_POST['title']; ?></title>
</head>
<body>

<p>
<b><?php echo $_POST['title']; ?></b><br>
Created by:<br> 
<?php echo $_POST['author']; ?><br>
<?php echo $_POST['company']; ?><br>
Contact: <br>
<?php echo $_POST['phone']; ?><br>
<?php echo $_POST['address']; ?><br>
Flyer Description: <br>
<?php echo $_POST['comments']; ?><br>
</p>

</body>
</html>

As you will see if you run this form, the function is pretty basic. Here is where I need the assistance. In the initial form the "shorturl" value is taken. The function of the shorturl value is as follows:

If this form was hosted on examplesite.com, then I would ultimately like for the form that is created to be available with submitted answers at examplesite.com/shorturl

First of all, how do I verify that this is in fact a single word via PHP? If a user submits the shorturl value as "House" then I need the form to return the value as true, but if the user submits "Big House" then the value is false and they need to alter the value to something that is acceptable such as "BigHouse"

Secondly, I need to verify that the shorturl value is unique to the site. In other words, once a shorturl has been used, that value needs to be sent to the MySQL database so that it will not be replicated by another user. To continue our example, if someone already had "House" as their shorturl value then the full URL of examplesite.com/House is already taken. Then if a new user comes and tries to use "House" the submission will produce an error message that says the name is taken.

And finally, how do I get all of this information to auto-generate a unique webpage with the form results? For an example let's continue examplesite.com/House Right now, when a user submits the form, the data is displayed on examplesite.com/contact.php. How do I generate a URL which would display the form data and be unique as defined by the shorturl and be viewable to a third party site visitor without submitting new data?

Wow. I hope that all makes sense.

I know there are several questions in here, so if you can only assist with one step that is fine. If you can tackle this entire issue then more power to you :)

I have done a fair amount of research on this and I am thinking that the first 2 questions should be able to be solved with PHP, but the third might involve a mod_rewrite function of some sort. I cannot thank you enough for getting this far with my query and many many thanks if you can provide a solution.

  • 写回答

2条回答 默认 最新

  • douping1825 2011-06-16 06:15
    关注

    This should do a good job of verifying $shorturl:

    if (preg_match('/[^a-z0-9]/i', $shorturl)) {
        // $shorturl contains characters other than just numbers or
        // letters such as a tab, space, or special chars you probably don't want
    }
    

    As for making sure the url is unique:

    if (!mysql_num_rows(mysql_query("SELECT id FROM contact WHERE url = '$shorturl' LIMIT 1")) {
        // it is unique, yay    
    }
    

    And you would insert the urls like so:

    mysql_query("INSERT INTO contact (url) VALUES ('$shorturl')");
    

    As for autogenerating the content, that shouldn't be that tricky. First, you will need to insert all the form data into the database, I would do this at the same time you insert the url. For dynamically retrieving the data, (using such a short url) you will need to do a tiny bit of .htaccess modification.

    Here is an example of what your .htaccess might look like for a user to be able to go to domain.com/shorturl while the actual scripts being ran (and what they will see) are at domain.com/contact.php?short_url=shorturl

    RewriteEngine on  
    
    # don't rewrite if the file exists
    RewriteCond %{REQUEST_FILENAME} !-f
    # don't rewrite if the directory exists
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ contact.php?short_url=$1
    

    At this point the rest is just capturing the GET variable as $_GET['short_url'] within contact.php (or anywhere you want this script to reside, as long as you change the RewriteRule accordingly) and returning the rest of the the information you captured using database queries, maybe something like:

    $short_url = mysql_real_escape_string($_GET['short_url']);
    
    $sql = "SELECT * FROM contact WHERE url = '$short_url'";
    $user_data = mysql_fetch_array(mysql_query($sql));
    
    extract($user_data);
    // with extract, all of $user_data's keys are now variables and their respective values
    // are contained within those variables
    // $user_data['company'] now becomes simply $company, for example
    
    echo "Company: $company";
    // etc...
    

    I hope this helps :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。