在PHP页面之间传递变量 [英] Transfer variables between PHP pages

查看:55
本文介绍了在PHP页面之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个页面上获得用户输入,将其存储在一个php变量中,并在另一个php页面中使用它.我已经尝试过使用会话",但是它似乎没有用.还有另一种安全的选择吗?此信息可能是用户名和密码.

I want to get user input in one page, store that in a php variable and use it in another php page. I have tried using 'sessions' but it doesn't seem to be working. Is there another safe alternative? This information is likely to be usernames and passwords.

推荐答案

尝试更改会话代码,因为这是实现此目的的最佳方法.

Try changing your session code as this is the best way to do this.

例如:

<?php
session_start();

if (isset($_POST['username'], $_POST['password']) {
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['password'] = $_POST['password'];
    echo '<a href="nextpage.php">Click to continue.</a>';
} else {
    // form
}
?>

nextpage.php

<?php
session_start();

if (isset($_SESSION['username'])) {
    echo $_SESSION['username'];
} else {
    header('Location: index.php');
}
?>

但是,我可能会在会话中存储更安全的内容(例如用户ID),而不是用户的登录凭据.

However I'd probably store something safer like a userid in a session rather than the user's login credentials.

这篇关于在PHP页面之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆