如何在没有PHP的连续登录页面上显示用户名?

时间:2015-03-23 03:54:22

标签: javascript php ajax url

我想在主页上显示用户名,依此类推"嗨,"从URL中提取。

<div id="login">
    <form method="post" action="">
        <h2>Login</h2>
        <p>
            <label for="username">Username: </label>
            <input type="text" name="username" /> </p>
        <p>
            <label for="pwd">Password: </label>
            <input type="password" name="pwd" /> </p>
        <p>
            <input type="submit" id="submit" value="Login" name="submit" /> </p>
    </form>

2 个答案:

答案 0 :(得分:0)

以下javascript代码将允许您从网址获取搜索变量,即。

www.example.com?query=search

将为您提供一个对象

{query: search}

//Code to get search string from urlParams
var match,
    pl     = /\+/g,  // Regex for replacing addition symbol with a space
    search = /([^&=]+)=?([^&]*)/g,
    decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
    query  = window.location.search.substring(1);

var urlParams = {};
while (match = search.exec(query)){
    urlParams[decode(match[1])] = decode(match[2]);
}   

我不能相信这段代码,它是来自另一个stackoverflow问题,我现在找不到它

答案 1 :(得分:0)

回答你的问题......你可以这样做:

创建其他页面以及#34;接收POST&#34;从登录,例如:

<强> login_user.php

<?php
session_start();

$username = null;
$password = null;

$homepage = 'http://www.your-website-url-here.com';

if ($_SERVER['RQEUEST_METHOD'] == 'POST') {
  if (!empty($_POST['username'])) {
    $username = $_POST['username'];
    $password = $_POST['password'];

    // check in database if this user and password matchs

    // put the username in session
    $_SESSION['msg'] = "Welcome $username";
    $_SESSION['username'] = $username;

    // then redirec to home
    header("Location:$homepage");
  } else {
    // some validatoin message to user
    $_SESSION['msg'] = "Validate the e-mail and password";

    // then redirect back
    header('Location: ' . $_SERVER['HTTP_REFERER']);
  }
} else {
  // is not a post request
  $_SESSION['msg'] = "You can't access this page";

  // put the username in session
  header('Location: ' . $_SERVER['HTTP_REFERER']);
}
exit;

然后,修改您的登录表单并将新的login_user.php放入action属性中:

<form method="post" action="login_user.php">

之后,您可以使用$_SESSION数组捕获用户名。

请记住将session_start();放入您的首页文件(第一行)


注意

你问题的标题是:没有php ,但你在评论中说我尝试使用$_SESSION,但我不能

所以...有或没有PHP?我的解决方案仅限于php。


观察/参加

这不是最好和安全的方法。

我只根据您的需要回答问题,而不是最好的方法。

我这样说是因为:您必须确保您的登录信息实际来自请求的页面,同时验证数据不允许sql注入或服务器文件和/或数据库中的任何其他类型的攻击。所以在继续工作之前先考虑一下。


希望这会对你有所帮助。

抱歉我的英文