drxv39706 2013-01-28 00:36
浏览 66
已采纳

PHP新手问题 - 登录和Cookie

I'm just starting with PHP, so sorry if I'm confusing.

I created my index page (index.php) by laying it out with HTML and Styling it with CSS.

I have a "couple" of questions that I would like to ask to get a basic idea of what I will need to look at or do.

So on my home page (index.php) I have a signup and login form. I'm not necessarily worried about the Sign Up form just yet, but more on the log in form. I also found a tutorial on how to do this, but it works a little differently than how I want it to work.

I need the user to log in and go to the control panel (maybe control.php). I'm guessing that's fairly simple. But if the user makes an error, I need the error message to display on the index page (under the log in form). So do I need to create a separate page named login.php to set the action on the form, or should I set the action to index.php and just put the log in code on the home page.

Another thing is a language changer. To start with, I'm offering this website in French, Spanish, and of course English. I have a selection box that onchange runs a javascript function that changes all of the text (using .InnerHTML) to that certain language, but I want to make sure the user doesn't have to do that every time they come to the website. I'm guessing you use a cookie for that. But how would I do this? By using a "form" so I can (on page load) check to see what language and go to www.mysite.com/index.php?english.. I'm completely new to this.. So basically, I'm asking how would you do this? Can I keep the Javascript function, or will I have to recode that to PHP (the text changer)?

Any comments, answers, or suggestions are appreciated!

Thanks! -Ryan

  • 写回答

2条回答 默认 最新

  • drdyf42880 2013-01-28 11:11
    关注

    Ideally we'd see some code of your own... However...

    I need the user to log in and go to the control panel (maybe control.php). I'm guessing that's fairly simple.

    1) You will need to name your HTML Form, and give it an action of index.php;

    See: http://www.w3schools.com/tags/tag_form.asp

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

    2) Name your UserName and Login Boxes with suitable ID's and Names;

    See: http://www.w3schools.com/tags/att_input_name.asp

    <input type="text" name="name"><br>
    <input type="text" name="password"><br>
    

    3) Add a Div Tag with some inline PHP to let your user know if their Username or Password was incorrect;

    See: http://www.w3schools.com/php/default.asp

    <div><?PHP echo $ErrorText ?></div>
    

    4) Add a Submit Button

    <input type="submit" value="Login">
    

    5) Add some PHP which reads the Login and Password Boxes PostBack values when the Form is Submitted;

    See: http://www.w3schools.com/php/php_post.asp

    $UserName = $_POST['name'];
    $Password= $_POST['password'];
    

    6) Add some Code and SQL to Check the username and password;

    See: http://php.net/manual/en/mysqli.quickstart.php

    $mysqli = new mysqli("localhost", "user", "password", "database");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        die();
    }
    
    if (!($stmt = $mysqli->prepare("SELECT * FROM Users WHERE UserName = '$UserName'))) {
        echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
        die();
    }
    
    if (!$stmt->execute()) {
        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
        die();
    }
    
    if (!($res = $stmt->get_result())) {
        echo "Getting result set failed: (" . $stmt->errno . ") " . $stmt->error;
        die();
    }
    
    if ($res->row_count > 1) {
        echo "Too many users!";
        die();
        }
    
    $row = $res->fetch_assoc();
    

    7) Check the Password and if correct, redirect to your Control Panel, otherwise report your error;

    if ($row['Password'] == $Password)
    {
        header('Location: http://www.example.com/control.php');
    }
    else
    {
        $ErrorText = "Incorrect Username or Password!";
    }
    

    You'll need to store your user info either as a cookie or as a session too. For this info see; http://www.w3schools.com/php/php_cookies.asp and http://www.w3schools.com/php/php_sessions.asp

    For the Language Changer, as Shomz says, there are many ways to do this. But using PHP to display all your text is the best method here.

    Please note... I've not tested any of the code above, so there may be the odd error... But this should get you going anyway!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?