我正在处理一些代码,为我的网站上的页面生成一些随机URL。我的意图是让这个人重定向到他们刚刚创建后创建的页面,但他们被转发到'switch',一个php代码,用于根据按下哪个单选按钮确定触发哪个函数。这是我的页面,允许他们'上传'到我的网站:为什么file_put_contents不创建页面?
<html>
<head>
<link rel="stylesheet" type="text/css" href="/style.css">
<title>
Learning Made Easy
</title>
</head>
<body>
<?php include_once 'googleanalytics.php'; ?>
<a href="http://terrythetutor.com">
<div class="banner"> </div>
</a>
<?php include 'menu.php'; ?>
<div class="content">
</br>
</br>
</br>
<form action="../scripts/switch.php" method="post">
Title:
</br><input type="text" name="Title">
</br>
</br>
</br>
Summary of the video (including questions used in the video):
</br><textarea name="Summary" COLS=60 ROWS=10></textarea>
</br>
</br>
</br>
URL of the video (Yes, this means you need to upload it to an external website.):
</br><input type="text" name="URL">
</br>
</br>
Which course does your video pertain to?</br>
<input type="radio" name="course" value="intermediate"> Intermediate and below</br>
<input type="radio" name="course" value="college"> College Algebra</br>
<input type="radio" name="course" value="precalculus"> PreCalculus</br>
<input type="radio" name="course" value="trigonometry"> Trigonometry</br>
<input type="radio" name="course" value="calculus I"> Calculus I</br>
<input type="radio" name="course" value="calculus II"> Calculus II</br>
<input type="radio" name="course" value="calculus III"> Calculus III</br>
<input type="radio" name="course" value="differential equations"> Differential Equations</br>
</br>
</br>
<input type="submit" value="Submit, foo!">
</form>
</br>
</br>
</br>
<p>
Please understand that you will not be able to change the title, summary, or URL of your video after submission.
</p>
</div>
<div class="footer">
<?php include 'footer.php'; ?>
</div>
</body>
</html>
这个位很好用。之后,他们被重定向到一个'switch.php'。你已经知道那是什么。
<?php
if (isset($_POST['course']) && $_POST['course'] == 'intermediate') {
include('storeintermediate.php');
} elseif (isset($_POST['course']) && $_POST['course'] == 'college') {
include('step2a.php');
} elseif (isset($_POST['course']) && $_POST['course'] == 'precalculus') {
include('replace.php');
} elseif (isset($_POST['course']) && $_POST['course'] == 'trigonometry') {
include('replace.php');
} elseif (isset($_POST['course']) && $_POST['course'] == 'calculus I') {
include('replace.php');
} elseif (isset($_POST['course']) && $_POST['course'] == 'calculus II') {
include('replace.php');
} elseif (isset($_POST['course']) && $_POST['course'] == 'calculus III') {
include('replace.php');
} elseif (isset($_POST['course']) && $_POST['course'] == 'differential equations') {
include('replace.php');
}
?>
从那里,我们去storeintermediate.php,在那里我有抓住所有的变量,它们存储在数据库中,与模板格式创建一个新的随机生成网页的功能,并提取URL他们正在上传的视频。
<?php
// Create connection
$con = mysqli_connect("*******","******","************","*");
$IP = $_SERVER['REMOTE_ADDR'];
$IP = mysqli_real_escape_string($con, $IP);
$Title = mysqli_real_escape_string($con, $_POST[Title]);
$Summary = mysqli_real_escape_string($con, $_POST[Summary]);
$URL = mysqli_real_escape_string($con, $_POST[URL]);
$number = mt_rand(100,99999999); // see $new_url
$nospace = str_replace(' ', '_', $Title); // to enable URL friendly titles
$new_url = $number . $nospace ; // to ensure that each URL is unique
$data = include('../template.php');
$embed_URL = substr($URL, -11);
file_put_contents("../videos/" . $new_url, $data, FILE_APPEND) ;
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO `Intermediate Algebra` (Title, URL, IP, Summary)
VALUES
('$Title','$URL','$IP','$Summary')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header('Location: http://terrythetutor.com/'.$new_url);
?>
现在,我的问题:当我提交测试页时,页面被创建。但大小是1个字节,文件中唯一的是'1'。我无法理解为什么。
其次,被重定向到的页面(也出于某种原因持有提交的内容)IS switch.php,但它确实应该是创建的页面。
第三(也可能与第二个有关),当我尝试重定向到新生成的页面时,我的头部出现错误。
任何帮助将不胜感激。非常感谢大家,
特里。
而不是每次使用框架(它将处理路由和不同的页面URL而不创建额外的文件)或使用一个基本的'page.php'来创建一个新文件,该页面将基于URL参数显示内容你的网址就像'page.php?page = some_page_id')。 – 2013-12-15 20:29:04
准备写@安德烈所说的。您不以这种方式创建页面,将数据存储在数据库中并加载与目标URL匹配的数据(使用模板)。除此之外,你发布的代码有很多错误(例如URL友好的代码片段,混合问题),所以我严重建议你看一下现有的框架之一(例如[Symfony 2](http:// symfony的.com /))。 – nietonfir
@nietonfir代码有什么问题?什么是网址友好的片段和“混合关注”? – user2666324