dongzhijing8202 2018-11-23 18:23
浏览 74

var_dump数组返回我的字符串,双?

After researching for hours, no proper fix has been found by myself so far, thus here I am!

I'm currently working on a school project that uses a login.php validator file to check if the user filled in all inputs, etc. etc., and also to create sessions.

require "config.inc.php";

    $query = ("SELECT username, password, active FROM *DATABASE* WHERE username='".$username."' AND password='".$password."' AND active='1'") or die(mysql_error());
    $result = mysqli_query($mysqli, $query);
    $match  = mysqli_num_rows($result);

    if($match > 0){
       //Directing to account page
       header("Location:account.php");

       //User info search
       $queryID = ("SELECT id FROM *DATABASE* WHERE username='".$username."' AND password='".$password."' AND active='1'") or die(mysql_error());
       $resultID = mysqli_query($mysqli, $queryID);
       $id = mysqli_fetch_assoc($resultID);

       $queryNAME = ("SELECT name FROM *DATABASE* WHERE username='".$username."' AND password='".$password."' AND active='1'") or die(mysql_error());
       $resultNAME = mysqli_query($mysqli, $queryNAME);
       $naam = mysqli_fetch_assoc($resultNAME);

       //Session user info
       $_SESSION['id'] = $id;
       $_SESSION['naam'] = $naam;

This is just a minor piece of the validator, but as you can see it redirects to the account page (This works fine), and should create sessions for the name and ID found in the database.

The account page then again receives and translates the sessions to variables with the following code;

  //SESSIONS
  require 'session.php'; //CHECKS IF SESSION IS VALID, IF NOT: RETURN TO HOME

  //VARIABLES
  if (isset($_SESSION['naam']) && isset($_SESSION['id']) && isset($_SESSION['username'])) {
    print_r($_SESSION['naam']); //Debug purposes
    print_r($_SESSION['id']);
    print_r($_SESSION['username']);
    $id = implode($_SESSION['id']); //Convert to String
    $naam = implode($_SESSION['naam']);
    $username = ($_SESSION['username']); //Already a string, this was a variable used in the POST form
    var_dump($naam);
  } else {
    echo "No sessions"; //Debug purposes
  }

When I echo the username on my account page, it displays the username perfectly fine, and I assume that's because username has always been a string to begin with.

Yet when I echo the naam variable, it displays "AdministratorAdministrator" even though the naam in the database is just Administrator.

This is my print_r result:

Array ( [0] => Administrator [name] => Administrator )
Array ( [0] => 23 [id] => 23 ) 
admin
string(26) "AdministratorAdministrator"

Any clues on how I can ensure that whenever I echo the naam variable, it shows just Administrator ONCE, and not twice? thanks!

  • 写回答

1条回答 默认 最新

  • dongzhou4727 2018-11-23 18:30
    关注

    You need to save onlye the data you need in the Session

    $_SESSION['id'] = $id['id'];
    $_SESSION['naam'] = $naam['name'];
    

    In your case you are saving the entire result from the function mysqli_fetch_assoc, which is an array, as you can see from the docs:

    http://php.net/manual/en/mysqli-result.fetch-assoc.php

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法