开发者

Remember fields across pages?

I have this: main.php:

ID: <input name="id" id="id" type="text" size="20" value="<?php echo $_POST['id']; ?>"><br>
Password: <input name="password" id="password" type="password" value="<?php echo $_POST['password']; ?>" size="20">

File main.php is index page.

File main.php开发者_开发知识库 is a form with action="main.php"

When i go from main.php to console.php, i dont get the values id and password.

When i go from main.php to plugins.php, i dont get the values id and password.

From main.php, you can get into plugins.php and console.php. But you don't get the values then.

I know why it happens.

How to fix this? How can i make, that when i go to console.php, or plugins.php, the fields will stay remembered?

Sessions are too hard for me to learn. Is there any other solution?


Too hard? Put simply, at the top of every page you want to use sessions with, use session_start(). Now, you want to save something to a session?

#page1
session_start();
$_SESSION['foo'] = 'bar';

#page2
session_start();
echo $_SESSION['foo'] #echoes bar

unset($_SESSION['foo']); #destroy foo
#if you want to discard the entire session, use
session_destroy(); #going down! 

So, in your case, maybe something like:

#main.php
session_start();
if (isset($_POST['id'], $_POST['password'])) {
    $_SESSION['id'] = $_POST['id'];
    $_SESSION['password'] = $_POST['password'];
}
#rest of main.php

#console.php and plugins.php
session_start();
if (isset($_SESSION['id'], $_SESSION['password']))
    #do stuff

I feel bad for spoon-feeding you with it, but basic session functionality is really all that's needed in the case you're describing, and it's probably the easiest way to pass around sensitive info, and is basically what sessions were made for.

For more info:

  • http://www.php.net/manual/en/reserved.variables.session.php
  • http://www.php.net/manual/en/function.session-start.php
  • http://www.phpriot.com/articles/intro-php-sessions


1) Learn sessions. And never pass around someone's password. You should hash the password, and check it against a hashed value in your database. When you have checked for a match, you can store in your session variables that the user is authenticated - you no longer need to know which password they submitted.

2) Understand that when you submit an html form, the action of the form is the only page that can do anything directly with the posted form parameters.

...

Try not to give up on learning these things - they were created to make your life easier, not more challenging.

Example PHP authentication classes for you to use

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜