var_dump array returning my string, double?
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!
php mysql arrays session login
|
show 3 more comments
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!
php mysql arrays session login
//header("Location:account.php");// You are redirecting before store values in session!!!, redirect should be at last line in your code. Also use exit; after redirect
– user1844933
Nov 23 '18 at 18:28
whoa! How awkward is that, thanks so much :)
– SessionPHP
Nov 23 '18 at 18:33
You also can't mix different mysql apis.
– Funk Forty Niner
Nov 23 '18 at 18:35
This is open to an SQL injection, use a prepared statement. Also, do not store plain text passwords, usepassword_hash()
.
– Funk Forty Niner
Nov 23 '18 at 18:37
1
Youror die(mysql_error()
code is on setting a string value, usually they should be on themysqli_query()
call (if indeed at all) and as @FunkFortyNiner pointed out it should bemysqli_error($mysqli)
.
– Nigel Ren
Nov 23 '18 at 18:39
|
show 3 more comments
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!
php mysql arrays session login
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!
php mysql arrays session login
php mysql arrays session login
edited Nov 23 '18 at 18:36
Funk Forty Niner
1
1
asked Nov 23 '18 at 18:23
SessionPHPSessionPHP
11
11
//header("Location:account.php");// You are redirecting before store values in session!!!, redirect should be at last line in your code. Also use exit; after redirect
– user1844933
Nov 23 '18 at 18:28
whoa! How awkward is that, thanks so much :)
– SessionPHP
Nov 23 '18 at 18:33
You also can't mix different mysql apis.
– Funk Forty Niner
Nov 23 '18 at 18:35
This is open to an SQL injection, use a prepared statement. Also, do not store plain text passwords, usepassword_hash()
.
– Funk Forty Niner
Nov 23 '18 at 18:37
1
Youror die(mysql_error()
code is on setting a string value, usually they should be on themysqli_query()
call (if indeed at all) and as @FunkFortyNiner pointed out it should bemysqli_error($mysqli)
.
– Nigel Ren
Nov 23 '18 at 18:39
|
show 3 more comments
//header("Location:account.php");// You are redirecting before store values in session!!!, redirect should be at last line in your code. Also use exit; after redirect
– user1844933
Nov 23 '18 at 18:28
whoa! How awkward is that, thanks so much :)
– SessionPHP
Nov 23 '18 at 18:33
You also can't mix different mysql apis.
– Funk Forty Niner
Nov 23 '18 at 18:35
This is open to an SQL injection, use a prepared statement. Also, do not store plain text passwords, usepassword_hash()
.
– Funk Forty Niner
Nov 23 '18 at 18:37
1
Youror die(mysql_error()
code is on setting a string value, usually they should be on themysqli_query()
call (if indeed at all) and as @FunkFortyNiner pointed out it should bemysqli_error($mysqli)
.
– Nigel Ren
Nov 23 '18 at 18:39
//header("Location:account.php");// You are redirecting before store values in session!!!, redirect should be at last line in your code. Also use exit; after redirect
– user1844933
Nov 23 '18 at 18:28
//header("Location:account.php");// You are redirecting before store values in session!!!, redirect should be at last line in your code. Also use exit; after redirect
– user1844933
Nov 23 '18 at 18:28
whoa! How awkward is that, thanks so much :)
– SessionPHP
Nov 23 '18 at 18:33
whoa! How awkward is that, thanks so much :)
– SessionPHP
Nov 23 '18 at 18:33
You also can't mix different mysql apis.
– Funk Forty Niner
Nov 23 '18 at 18:35
You also can't mix different mysql apis.
– Funk Forty Niner
Nov 23 '18 at 18:35
This is open to an SQL injection, use a prepared statement. Also, do not store plain text passwords, use
password_hash()
.– Funk Forty Niner
Nov 23 '18 at 18:37
This is open to an SQL injection, use a prepared statement. Also, do not store plain text passwords, use
password_hash()
.– Funk Forty Niner
Nov 23 '18 at 18:37
1
1
Your
or die(mysql_error()
code is on setting a string value, usually they should be on the mysqli_query()
call (if indeed at all) and as @FunkFortyNiner pointed out it should be mysqli_error($mysqli)
.– Nigel Ren
Nov 23 '18 at 18:39
Your
or die(mysql_error()
code is on setting a string value, usually they should be on the mysqli_query()
call (if indeed at all) and as @FunkFortyNiner pointed out it should be mysqli_error($mysqli)
.– Nigel Ren
Nov 23 '18 at 18:39
|
show 3 more comments
1 Answer
1
active
oldest
votes
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
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451425%2fvar-dump-array-returning-my-string-double%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
add a comment |
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
add a comment |
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
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
answered Nov 23 '18 at 18:30
Felippe DuarteFelippe Duarte
10.6k21624
10.6k21624
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53451425%2fvar-dump-array-returning-my-string-double%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
//header("Location:account.php");// You are redirecting before store values in session!!!, redirect should be at last line in your code. Also use exit; after redirect
– user1844933
Nov 23 '18 at 18:28
whoa! How awkward is that, thanks so much :)
– SessionPHP
Nov 23 '18 at 18:33
You also can't mix different mysql apis.
– Funk Forty Niner
Nov 23 '18 at 18:35
This is open to an SQL injection, use a prepared statement. Also, do not store plain text passwords, use
password_hash()
.– Funk Forty Niner
Nov 23 '18 at 18:37
1
Your
or die(mysql_error()
code is on setting a string value, usually they should be on themysqli_query()
call (if indeed at all) and as @FunkFortyNiner pointed out it should bemysqli_error($mysqli)
.– Nigel Ren
Nov 23 '18 at 18:39