使用 JS 创建一个 url 实例

Creating a url instance with JS

我正在将一个简单的 HTML5 虚拟 class 房间整合到语言教学网站上。

我已经复制了 class 房间的源代码并将其粘贴到以下文件夹中的 index.html 页面中...

网站.com/classes/room-1

这工作正常,但是任何人都可以随时进入 class 房间,我需要限制对 classes 的访问。

我想通过制作一个独特的 url 实例来实现这一点,所以基本上如果您没有 url link 您就无法访问 class房间。所以 url 看起来像这样...

站点.com/classes/room-1?实例=l23jhvn23o1i2un3lnj12xas

然后,当一位教师使用他的教师姓名登录到他的 class 房间时,将生成一个唯一的 URL,并将该教师重定向到该唯一的 URL 实例。他可以通过电子邮件与学生分享 link,他们可以继续 class。

这是处理实例 url 的代码片段...那么生成 URL 实例的最佳方法是什么?

function start()
{
    // Optional username and password. If not specified here, a popup dialog
    // box will appear.
    var username = '';
    var password = '';

    // The Groupworld server to connect to. You can optionally specify the
    // port number (using the format "server:port").
    var base = 'www.groupworld.net:9175:1639';

    // The object to load and instance name. To create a different "session",
    // just copy the html page and change the instance name.
    var object = 'new_conference:room-1';

    // Flags: not currently used.
    var flags = 0;
    groupworld.startup(username, password, base, object, flags);
}

这是我们需要的图形说明...

Teacher work flow

提前致谢,

叶忒罗

我想我可能已经自己回答了这个问题,如果有人愿意检查我的逻辑,我将不胜感激。

关键在课堂里嵌入代码...

// The object to load and instance name. To create a different "session",

// just copy the html page and change the instance name.

var object = 'new_conference:jethro';

所以每次老师创建教室时,我都需要将Javascript中的'var object'从'jethro'(这就是我命名教室的名称)更改为随机字符串。该随机字符串应保存在 PHP 变量中,我将其称为“$instance”...

所以我创建了一个带有临时数据库的 index.php 登录页面。这是实时站点上的 link:http://toucan-talk.com/classes

临时数据库具有以下凭据

用户名:jethro

密码:密码

这是 index.php 代码,其中包含我的注释...

<?php 
session_start();

if ($_POST['username']) {

        // Temporary database
    $dbUsname = "jethro";
    $dbPaswd = "password";
    $uid = "1";

    $usname = strip_tags($_POST['username']);
    $paswd = strip_tags($_POST['password']);

    if ($usname == $dbUsname && $paswd == $dbPaswd) {
        // Set session variables
        $_SESSION['username'] = $usname;
        $_SESSION['id'] = $uid;
        // To generate the random string from a word
        $str = "Hello"; // word to hash
        $instance = md5($str); // hash stored in variable
        header('Location: classroom.php?instance='.$instance);// Hash is concatenated onto the login redirect page
    } else {
        echo "<p>Oops! That username or password combination is incorrect. Please try again.</p>";
    }
}

?>



<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="style.css">
<title>Toucan-Talk Tutor Login</title>
</head>

<body>
    <div id="wrapper">
        <center>
            <img style="width: 250px;" alt="" src="http://www.toucan-talk.com/images/logos/footer-logo.png">
        </center>
        <h1>Enter Classroom</h1>
        <form id="form" action="index.php" method="post" enctype="multipart/form-data">
        Username: <input type="text" name="username"/><br>
        Password: <input type="password" name="password" /><br>
        <br>
        <center>
            <input class="button" type="submit" value="Enter" name="Submit" />
        </center>
        </form>
</body>
</html>

所以我将 md5 散列连接到 header 页面重定向,这样我现在得到一个 url,看起来像这样:http://toucan-talk.com/classes/classroom.php?instance=8b1a9953c4611296a827abf8c47804d7

我创建了另一个名为 classroom.php 的文件,header 重定向到该文件。里面有教室嵌入代码。这只是该文件中的 PHP:

<?php 
session_start();

// Variable with the md5() hash code
$instance =  $_GET['instance']; 

if (isset($_SESSION['id'])) {
    $uid = $_SESSION['id'];
    $usname = "Test variables: <br> Username: ".$usname. "<br> Id: ".$uid;
} else {
    echo 'Please login';
    die();
}

?>

如您所见,我再次将变量“$instance”定义为 $instance = $_GET['instance']; (我不太确定它仍然有什么作用,但没有它就无法工作!)

所以我知道 $_GET['instance'] = $instance = 8b1a9953c4611296a827abf8c47804d7

现在我只需用 PHP 标签中的变量 $instance 替换 Javascript 'var object' 即可在 Groupworlds 服务器上即时生成教室!

// The object to load and instance name. To create a different "session",
// just copy the html page and change the instance name.
var object = 'new_conference:<?php echo $instance ?>';

现在,当我查看课堂源代码时,实例是 md5() 哈希!

sourcecode

这似乎可以解决问题...那么这似乎是解决此编码问题的合理方法吗?我很高兴对此进行改进以获得可行的东西。

非常感谢你们到目前为止的帮助。

叶忒罗

Groupworld 现在有一个示例开源 php/mysql 在线辅导网站,这可能会有所帮助:

https://github.com/groupboard/tutor-scheduler