我试图在登录后显示用户名,但我不知道该怎么做.. 这是我的登录表单..
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="simplestyle.css">
</head>
<body>
<div class="divOne">
<h1>Login</h1>
<form action="simple2.php" method="post">
<table style="width:60%">
<tr>
<td class="colm1"><b>Name:</b></td>
<td class="colm2"><input type="text" name="username"></td>
</tr>
<tr>
<td class="colm1"><b>Password:</b></td>
<td class="colm2"><input type="password" name="password"></td>
</tr>
</table><br>
<input type="submit" name="login" value="Post!" class="buttonLog"> <br />
</form>
</div><br>
</body>
</html>
这是我登录的连接..我将php文件命名为simple2.php
<?php
session_start();
if (isset($_POST['login'])){
$_SESSION['uname'] = 'username';
$filename = "accounts.txt";
$uname=$_POST['username'];
$pwd=$_POST['password'];
$admin= 'hanna';
$adminPass= '2302';
if (file_exists($filename)) {
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$contents = file_get_contents($filename);
$contents = explode("\n", $contents);
if ($uname == $admin && $pwd == $adminPass){
header('Location: hometry.php');
}
elseif ($uname == $uname && $pwd == $uname) {
echo "member";
}
else{
echo "Username and Password is not registered!";
}
}
}
fclose($handle);
?>
登录后,我希望用户名显示在主页上,但我不知道该怎么做。有人能帮助我吗?
答案 0 :(得分:1)
因为你在simple2.php中分配了$ _SESSION ['uname'] ='username',然后在hometry.php中使用
调用变量<?php
session_start();
$uname = $_SESSION['uname'];
?>
但是从您的代码中,您需要输入
$uname=$_POST['username'];
前
$_SESSION['uname'] = 'username';
并将$ uname分配给$ _SESSION ['uname']否则它将始终显示'username' 或者您可以将用户名作为URL变量传递
header('Location: hometry.php?uname='.$uname);
因此修改后的代码对于选项1
将如下所示<?php
session_start();
if (isset($_POST['login'])){
$filename = "accounts.txt";
$uname=$_POST['username'];
$_SESSION['uname'] = $uname;
$pwd=$_POST['password'];
$admin= 'hanna';
$adminPass= '2302';
if (file_exists($filename)) {
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$contents = file_get_contents($filename);
$contents = explode("\n", $contents);
if ($uname == $admin && $pwd == $adminPass){
header('Location: hometry.php');
}
elseif ($uname == $uname && $pwd == $uname) {
echo "member";
}
else{
echo "Username and Password is not registered!";
}
}
}
fclose($handle);
?>
和选项2
<?php
session_start();
if (isset($_POST['login'])){
$filename = "accounts.txt";
$uname=$_POST['username'];
$pwd=$_POST['password'];
$admin= 'hanna';
$adminPass= '2302';
if (file_exists($filename)) {
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
$contents = file_get_contents($filename);
$contents = explode("\n", $contents);
if ($uname == $admin && $pwd == $adminPass){
header('Location: hometry.php?uname='.$uname);
}
elseif ($uname == $uname && $pwd == $uname) {
echo "member";
}
else{
echo "Username and Password is not registered!";
}
}
}
fclose($handle);
?>
答案 1 :(得分:0)
在您检查用户是否在您的accounts.txt文件中之后,立即将用户名放入会话中。
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { ApolloProvider } from 'react-apollo';
import { ApolloClient } from 'apollo-client';
import { SchemaLink } from "apollo-link-schema";
import { InMemoryCache } from 'apollo-cache-inmemory';
import { ApolloCache } from 'apollo-cache';
import gql from 'graphql-tag';
import { graphql } from 'react-apollo';
import { MockedProvider } from 'react-apollo/test-utils';
import { makeExecutableSchema, addMockFunctionsToSchema, MockList } from 'graphql-tools';
const typeDefs = `
type Query {
me: Person!
}
type Person {
name: String!
}
`;
const schema = makeExecutableSchema({ typeDefs });
addMockFunctionsToSchema({ schema });
const schemaLink = new SchemaLink(schema);
const client = new ApolloClient({
link: schemaLink,
cache: new InMemoryCache()
});
class App extends Component {
render() {
if (this.props.query && this.props.query.loading) {
return <div>Loading...</div>
}
if (this.props.query && this.props.query.error) {
return <div>Error: {this.props.query.error.message}</div>
}
const person = this.props.query.me;
return <div> {person.name} </div>
}
}
const personQuery = gql`
query PersonQuery {
me {
__typename
name
}
}
`;
const personQueryMockedData = {
me: {
__typename: 'String',
name: 'Gabe'
}
}
App = graphql(personQuery, { name: 'query' })(App);
ReactDOM.render(
<MockedProvider mocks={
[
{
request: { query: personQuery, variables: { cache: false } },
result: { data: personQueryMockedData }
}
]
}>
< App />
</MockedProvider>
, document.getElementById('root')
);
在你的homepage.php中调用用户名:
$_SESSION['uname'] = $uname;