/    Sign up×
Community /Pin to ProfileBookmark

Why Array Leaks Memory On Every Page Reload ?

Ok. This has been doing my head-in for 2hrs now.
I am trying to save to array all the secured passwords generated.
Everytime I refresh the browser, a new secure password gets generated and gets saved to array.
Problem is, all previous saved data from array gets deleted on every page refresh. Why is that ? Tried saving to session but no luck.
Look …
Notice the comments …
I even tried concatenating but no luck. Like so:

[code]
$secured_passwords_array[] .= $secured_password; //Dump current secured password to array.
[/code]

[code]
if(!session_id())
{
session_start();
}

$password = ‘password’;
$salts = array(‘121′,’gulf’,’¬’,’;@/’);
$salts_count = count($salts)-1;

echo ‘Line: ‘ .__LINE__; echo ‘<br>’;
echo $rand = rand(0,$salts_count); echo ‘<br>’;

if(EMPTY($_SESSION[‘secured_passwords_array’]))
{
echo ‘Line: ‘ .__LINE__; echo ‘<br>’;
$secured_passwords_array = array();
//die;
}

echo ‘Current hashed salted password: ‘.$hashed_salted_password = hash(‘sha256’,$password.$salts[$rand]);; echo ‘<br>’;
echo ‘Line: ‘ .__LINE__; echo ‘<br>’;

echo ‘Current secured password: ‘.$secured_password = password_hash($hashed_salted_password,PASSWORD_DEFAULT); echo ‘<br>’;
$secured_passwords_array[] .= $secured_password; //Dump current secured password to array.

$_SESSION[‘secured_passwords_array’] = $secured_passwords_array; //Saving to session incase browser refreshing erases all data saved to array.
echo ‘Line: ‘ .__LINE__; echo ‘<br>’;

//1st Attempt to recall all secured passwords saved to array.
foreach($secured_passwords_array AS $secured_password_submitted)
{
echo ‘secured password submitted: ‘.$secured_password_submitted; echo ‘<br>’; //RESULT: Failing to show all saved data from array.
}

//2nd Attempt to recall all secured passwords saved to array.
foreach($_SESSION[‘secured_passwords_array’] AS $secured_password_submitted)
{
echo ‘secured password submitted: ‘.$secured_password_submitted; echo ‘<br>’; //RESULT: Failing to show all saved data from array.
}

echo ‘All secured passwords submitted: ‘; print_r($secured_passwords_array); echo ‘<br>’;
echo ‘All secured passwords submitted: ‘; print_r($_SESSION[‘secured_passwords_array’]); echo ‘<br>’;
echo ‘Line: ‘ .__LINE__; echo ‘<br>’;
[/code]

What the heck is wrong ?
4am here. Going to bed very gloomy!

to post a comment
PHP

87 Comments(s)

Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Ignore
Copy linkTweet thisAlerts:
@NogDogAug 10.2022 — > @novice2022#1645737 all previous saved data from array gets deleted on every page refresh. Why is that ? Tried saving to session but no luck.

Because once a script completes, everything goes away. Using a session is one way to persist data between calls to the script. (A database would be another alternative.) So concentrate on the session approach and get rid of everything not centered around it, then if you're still having a problem: _show us only the smallest bit of code that demonstrates your problem, rather than expecting us to read through all those lines of multiple attempts and comments_. Please.
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — To add to Nogdog's comments - Why would anyone want to handle a group of hashed (or even un-hashed) passwords? That's putting them into a place where they could possibly be exposed. Any single password s/b safely stored and only accessed when it needs to be found and immediately released afterwards. I cannot think of anyone having a need to accumulate a group of them or even need to search for more than one at any time.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645753

I am just experimenting SALTing. Trying to generate different passwords based on different SALTs. That is main purpose.

I just wanted to dump to array so I do not have to write long codes to dump to db and extract from db.

It's all about fiddling with SALTing. Nothing more.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @NogDog#1645752

Ok. Bare min for you then ....

I gave the other part to Ginerjm,

What is wrong with my coding that it deletes array data ?

<i>
</i>echo 'Current hashed salted password: '.$hashed_salted_password = hash('sha256',$password.$salts[$rand]);; echo '&lt;br&gt;';

echo 'Current secured password: '.$secured_password = password_hash($hashed_salted_password,PASSWORD_DEFAULT); echo '&lt;br&gt;';
$secured_passwords_array[] .= $secured_password; //Dump current secured password to array.

$_SESSION['secured_passwords_array'] = $secured_passwords_array; //Saving to session incase browser refreshing erases all data saved to array.

//1st Attempt to recall all secured passwords saved to array.
foreach($secured_passwords_array AS $secured_password_submitted)
{
echo 'secured password submitted: '.$secured_password_submitted; echo '&lt;br&gt;'; //RESULT: Failing to show all saved data from array.
}

//2nd Attempt to recall all secured passwords saved to array.
foreach($_SESSION['secured_passwords_array'] AS $secured_password_submitted)
{
echo 'secured password submitted: '.$secured_password_submitted; echo '&lt;br&gt;'; //RESULT: Failing to show all saved data from array.
}



Thanks
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645750

And bare min for you.

I gave the other part to NogDog,

What is wrong with my coding that it deletes array data ?
<i>
</i>echo 'Current hashed salted password: '.$hashed_salted_password = hash('sha256',$password.$salts[$rand]);; echo '&lt;br&gt;';

echo 'Current secured password: '.$secured_password = password_hash($hashed_salted_password,PASSWORD_DEFAULT); echo '&lt;br&gt;';
$secured_passwords_array[] .= $secured_password; //Dump current secured password to array.

$_SESSION['secured_passwords_array'] = $secured_passwords_array; //Saving to session incase browser refreshing erases all data saved to array.

//1st Attempt to recall all secured passwords saved to array.
echo 'All secured passwords submitted: '; print_r($secured_passwords_array); echo '&lt;br&gt;';

//2nd Attempt to recall all secured passwords saved to array.
echo 'All secured passwords submitted: '; print_r($_SESSION['secured_passwords_array']); echo '&lt;br&gt;';

Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Do you ever write code for anything real?
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645757

What ? This ain't serious ....

<i>
</i>
$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;

echo 'Current hashed salted password: '.$secured_password = hash('sha256',$password.$salts[$rand]);; echo '&lt;br&gt;';
$secured_passwords_array[] .= $secured_password; //Dump current secured password to array.

$_SESSION['secured_passwords_array'] = $secured_passwords_array;

echo 'All secured passwords submitted: '; print_r($_SESSION['secured_passwords_array']); echo '&lt;br&gt;';



What is wrong with above code that array values get deleted on every page refresh unless you want me concatenating here too ....
<i>
</i>$_SESSION['secured_passwords_array'] .= $secured_passwords_array;


The previous post code was a browser auto typo as I lost connection and glitches.

So ignore it.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @NogDog

Ok, Shortest as possible ...

<i>
</i>$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;

echo 'Current hashed salted password: '.$secured_password = hash('sha256',$password.$salts[$rand]);; echo '&lt;br&gt;';
$secured_passwords_array[] .= $secured_password; //Dump current secured password to array.

$_SESSION['secured_passwords_array'] = $secured_passwords_array;

foreach($_SESSION['secured_passwords_array'] AS $secured_password_submitted)
{
echo 'secured password submitted: '.$secured_password_submitted; echo '&lt;br&gt;'; //RESULT: Failing to show all saved data from array.
}



What is wrong with above code that array values get deleted on every page refresh unless you want me concatenating here too ....
<i>
</i>$_SESSION['secured_passwords_array'] .= $secured_passwords_array;


The previous post code was a browser auto typo as I lost connection and glitches occured on code getting copied into browser then pasted into forum.

So ignore it.
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — No it is not serious. If it was, you would have written and put it into wherever you needed it and moved on instead of all this jibber-jabber about the best way to do for the last 17 yours.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @NogDog

@ginerjm

Nope! This was a big error on my part and I should not try concatenating an array:
<i>
</i>$_SESSION['secured_passwords_array'] .= $secured_passwords_array;


Anyway, without concatenating this, I still get array datas deleted on every page refresh.

Big puzzle!

$_SESSION should not have deleted the array datas!
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645760

I must learn what is leaking the ram memory. Ok ?

I get the feeling you yourself do not know the answer as you found no error in my code and are scratching your head yourself.

When I face an obstacle, I do not take shortcuts but try to diagnose the problem. Or, how else can you become a software doctor ?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — The memory is not leaking. Where do you get that idea?

Get rid of the session id test and simply start your session. If you have anything saved in the session it will (magically) re-appear for you.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645763

Ok. Thanks.

But why was session id test causing the issue ? I was checking if the session existed or not as on the very first page load it won't exist and would need to be created.

And, I guess this code is wrong too and I should get rid of the dot. Yes or no ?
<i>
</i>$secured_passwords_array[] .= $secured_password;

I think I should rid the dot as I am not joining strings. But getting rid of it still solves no issue and so let me try ridding the session id. Like you said.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — Ooos!

I just remembered that session_start() must be above all php code plus html. And so, this line is actually an error like Ginerjm spotted!

<i>
</i>session_start();
Copy linkTweet thisAlerts:
@NogDogAug 10.2022 — > @novice2022#1645755 //1st Attempt to recall all secured passwords saved to array.

I already told you this is not going to work, so get rid of that section if you're going to do this as a web page.

> @novice2022#1645755 echo 'Current secured password: '.$secured_password = password_hash($hashed_salted_password,PASSWORD_DEFAULT); echo '&lt;br&gt;';

I already told you elsewhere that it makes no sense to password_hash() a hash() result, so stop doing it. Choose one or the other to do your hashing, whichever works best for what you need to do with the results.

> @novice2022#1645754 It's all about fiddling with SALTing

Why? Do you not trust that the PHP functions work? If you want to see what they do, just write a little script you can run from the command line, and change values as needed. No reason to make a web page with things stored in session variables.
[code=php]
<-? php // remove the hyphen!

$passwords = [
'badpassword',
'BetterPassword2',
'Akc23%asjX9_16Qxd'
];

$salts = [
'1357',
'ABcd',
'Ab1Cd2'
];

foreach($passwords as $password) {
foreach($salts as $salt) {
printf(
"The hash of '%s' with salt '%s' is '%s'n",
$password,
$salt,
hash('sha256', $password . $salt)
);
}
}
[/code]

Run it on your computer from the command line:
[code=text]
$ php whatever-you-saved-it-as.php
The hash of 'badpassword' with salt '1357' is '9bca97b8b0c89cd8bcfcf669279f52361f40b150059beb161b07289d3bef65f4'
The hash of 'badpassword' with salt 'ABcd' is '46d5abd162be13c1fde3a67483127b7d7fb6844c35438bc3b0947ff9b765ef78'
The hash of 'badpassword' with salt 'Ab1Cd2' is 'a9bcaf01ad2c31bb3b8d206f8c3a7868697bd0a2111ebaf83fceaff485cf5c03'
The hash of 'BetterPassword2' with salt '1357' is '3022f6d0508328310636ce4504db9183ffecc89bf1f703382c4850b76679d4a9'
The hash of 'BetterPassword2' with salt 'ABcd' is '1c40bd918d3f2b61495453810243de17adb4883ad159812d61541a87699a030c'
The hash of 'BetterPassword2' with salt 'Ab1Cd2' is '04840aa270d3e8c464f68c3d52b183304997713c20cb3cf63b2e695c12dfd964'
The hash of 'Akc23%asjX9_16Qxd' with salt '1357' is 'fee1f095392d6de316598625e38e2b037aeb397cf10afa3d259f271b095f25cd'
The hash of 'Akc23%asjX9_16Qxd' with salt 'ABcd' is '0ddcf26bbf70c27adb82702bdee9514c4fc03d4e89b6e3cff018ac4abc96fafe'
The hash of 'Akc23%asjX9_16Qxd' with salt 'Ab1Cd2' is 'd2a13abe3b2fac8974dd67a20169bcace132a258816be578e902bdbebe663346'
[/code]

Yep, the hash() function works as advertised at https://php.net/hash . Who would have thought it?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — A session must be started before you can reference it at all. It has nothing to do with html. And you can always find the current PHP session data (from your last execution within your browser's session) by that start. RTFM.

Appending items to an array does not involve concatenation. The mere presence of the [] is what creates a new element for your array.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @NogDog#1645766

I do not use command line as do not know how to use it. DOS like stuffs, I avoid like plague.

If you check my latest code to you and Ginerjm then you will see I rid the password_hash() for the hash().

One question, this not showing error:
<i>
</i>foreach(array($_SESSION['secured_passwords_array']) AS $secured_password_submitted)
{
echo 'secured password submitted: '.$secured_password_submitted; echo '&lt;br&gt;'; //RESULT: Failing to show all saved data from array.
}

Is the array() part valid ?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Why is that 'array' word even there? And why do you need 2 echo statements to ouptut one line? Wasted instructions, slower code, more tedious maintenance......
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645769

Ok. Get you.

But tell me, if at first page load, I create an array.
<i>
</i>$secured_passwords_array = array();


Then when I reload the page and that same code line runs, then would the array get created again and erase all previous data from array. Yes or no ?

If so, then best I keep this line ....
<i>
</i>if(!ISSET($_SESSION['secured_passwords_array']) OR EMPTY($_SESSION['secured_passwords_array']))
{
echo 'Line: ' .__LINE__; echo '&lt;br&gt;';
$secured_passwords_array = array();
}


Else replace it with just:
<i>
</i>$secured_passwords_array = array();


What is your advice ?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Any var that you create during an execution goes away at the end of that execution. If you however decide you want to keep it you need to store that data somewhere. As was mentioned much much earlier that means putting it into your session storage or writing it to a database. That is your answer.

And once again you use 2 echos to output 1 line of output. Such silliness.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @NogDog

Correct me if I am wrong, but I think you taught me not to write like this:
<i>
</i>if(!ISSET($_SESSION['secured_passwords_array']) OR EMPTY($_SESSION['secured_passwords_array']))
{
echo 'Line: ' .__LINE__; echo '&lt;br&gt;';
$secured_passwords_array = array();
}


But this instead:
<i>
</i>if(EMPTY($_SESSION['secured_passwords_array']))
{
echo 'Line: ' .__LINE__; echo '&lt;br&gt;';
$secured_passwords_array = array();
}

So should I stick to this then ?

And, is it not better to shorten this:
<i>
</i>$secured_passwords_array[] = $secured_password; //Dump current secured password to array.
$_SESSION['secured_passwords_array'] = $secured_passwords_array[]; //Save array to session.


to this ?
<i>
</i>$_SESSION['secured_passwords_array'] = $secured_passwords_array[] = $secured_password; //Dump current secured password to array. Then save to session.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645771

Shortest as possible.

Any silly line sstill ?

<i>
</i>session_start();

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);

if(EMPTY($_SESSION['secured_passwords_array']))
{
echo 'Line: ' .__LINE__ .'&lt;br&gt;';
$secured_passwords_array = array();
}

echo 'Current hashed salted password: '.$secured_password = hash('sha256',$password.$salts[$rand]).'&lt;br&gt;';
echo 'Line: ' .__LINE__ .'&lt;br&gt;';

//echo 'Current secured password: '.$secured_password = password_hash($hashed_salted_password,PASSWORD_DEFAULT); echo '&lt;br&gt;';
$_SESSION['secured_passwords_array'] = $secured_passwords_array[] = $secured_password; //Dump current secured password to array.

echo 'All secured passwords submitted: ';
print_r($_SESSION['secured_passwords_array']).'&lt;br&gt;';
echo 'Line: ' .__LINE__ .'&lt;br&gt;';
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — I would start with this:


session_start();<br/>
if (isset($_SESSION['myarray']))<br/>
$myarray = $_SESSION['myarray'];<br/>
else<br/>
$myarray = array();


From that point on in my script I would use the local var to build whatever I am building and then save it in the session var version ONCE when I was done.
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Any silly line sstill ?

To answer you: The whole topic is silly.

Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645775

Ok. Before I change it to your code.

Let's try mine for one last time.

Now, tell me, what is wrong with this .....

<i>
</i>//SAMPLE 5A
session_start();

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);

if(EMPTY($_SESSION['secured_passwords_array']))
{
$secured_passwords_array = array();
}

echo 'Current hashed salted password: '.$secured_password = hash('sha256',$password.$salts[$rand]).'&lt;br&gt;';

$_SESSION['secured_passwords_array'] = $secured_passwords_array[] = $secured_password; //Dump current secured password to array.

echo 'All secured passwords submitted: ';
var_dump($_SESSION['secured_passwords_array']).'&lt;br&gt;';


Do you see anything wrong in the code that it would erase the array datas on every page load ?

Still not working.

Now, let us change the var_dump to:
<i>
</i>//SAMPLE 5B
session_start();

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);

if(EMPTY($_SESSION['secured_passwords_array']))
{
echo 'Line: ' .__LINE__ .'&lt;br&gt;';
$secured_passwords_array = array();
}

$secured_passwords_array = array();

echo 'Current hashed salted password: '.$secured_password = hash('sha256',$password.$salts[$rand]).'&lt;br&gt;';
echo 'Line: ' .__LINE__ .'&lt;br&gt;';

$_SESSION['secured_passwords_array'] = $secured_passwords_array[] = $secured_password; //Dump current secured password to array.

echo 'All secured passwords submitted: ';
$secured_passwords_array = $_SESSION['secured_passwords_array'];
var_dump($secured_passwords_array).'&lt;br&gt;';

Still no luck, mate!

I will now try your code but do see if you can spot errors on these 2 codes of mine as I can see none. Hence, the array datas should not have got deleted. Only shows one entry on array, no matter how many times I refresh the page and how many times I generate and save new passwords to the array.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645774

I now get a new error with your code.

Get echoed:

**Current hashed salted password: 35b40e725f79eebee4d02e8faec9b81c683c5f9aa9ed4457d0cf0ee89ffb9c0c

Fatal error: Uncaught Error: [] operator not supported for strings in C:xampphtdocsWorkgulpTemplatespassword_TEMPLATE.php:23 Stack trace: #0 {main} thrown in C:xampphtdocsWorkgulpTemplatespassword_TEMPLATE.php on line 23**

<i>
</i>//SAMPLE 5C
session_start();
if (isset($_SESSION['secured_passwords_array']))
$secured_passwords_array = $_SESSION['secured_passwords_array'];
else
$secured_passwords_array = array();

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);

echo 'Current hashed salted password: '.$secured_password = hash('sha256',$password.$salts[$rand]).'&lt;br&gt;';

$_SESSION['secured_passwords_array'] = $secured_passwords_array[] = $secured_password; //Dump current secured password to array.

$secured_passwords_array = $_SESSION['secured_passwords_array'];
echo 'All secured passwords submitted: '.'&lt;br&gt;';
var_dump($secured_passwords_array);


Let's see how you actually intended the code to be from top to bottom.
Copy linkTweet thisAlerts:
@sibertAug 10.2022 — > @novice2022#1645758 that array values get deleted on every page refresh

You have to store the array in a session or localStorage (or whatever you are familiar with). The array variable is only in the memory. Refreshing clear the memory.

https://jsfiddle.net/zb1ghrao/
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Here's how I would write this SHOULD I EVER THINK IT NECESSARY:

$password = 'password';

$salts = array('121','gulf','¬',';@/');

$salts_count = count($salts)-1;

$rand = rand(0,$salts_count);

if(isset($_SESSION['secured_passwords_array']))

$_
SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256',$password.$salts[$rand]);

echo "Current hashed salted password: $secured_password<br>";

$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.

echo 'All secured passwords submitted: ';

echo "<pre>", print_r(($_
SESSION['secured_passwords_array'],true), "</pre>";
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — The underscores seem to be missing in my SESSION var usages. Please add to yours
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @sibert#1645778

How come I never had this issue before ?

So, I write code like you showed and upload it to my website ?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Do not do what sibert is suggesting.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645780

Yes, I noticed. The forum changes it for security reason.

Thanks for the code.

Now, I get this error. I not know how to fix your final line:

<i>
</i>$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);
if(isset($SESSION['secured_passwords_array']))
$SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256',$password.$salts[$rand]);
echo "Current hashed salted password: $secured_password&lt;br&gt;";
$SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
echo 'All secured passwords submitted: ';
echo "&lt;pre&gt;", print_r(($SESSION['secured_passwords_array'],true), "&lt;/pre&gt;";

I usually do not write commas like the line above. Have not come to php tutorial on that. And so, I can't fix that line, it's error.

Parse error: syntax error, unexpected token "," in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 47

Your line:
<i>
</i>echo "&lt;pre&gt;", print_r(($SESSION['secured_passwords_array'],true), "&lt;/pre&gt;";


My edit no good:
<i>
</i>echo "&lt;pre&gt;"; print_r(($SESSION['secured_passwords_array'],true)); "&lt;/pre&gt;";


EDIT:

This seems to erase the error:
<i>
</i>echo "&lt;pre&gt;"; print_r($SESSION['secured_passwords_array'],true); "&lt;/pre&gt;";

Is this line correct ?
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645782

Cos he meant it for localhost only and not for website ?
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Ok my bad on this line. Note the !

if(!isset($_SESSION['secured_passwords_array']))

And the last line has 2 left parens. Drop one.
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Change my commas to periods. Nothing else.
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm

Mope mate!

Your code behaving like my code.

At first page load, it generates a password and dumps it to array and echoes it.

Then when I refresh the page and a 2nd password gets generated, then that gets dumped to the array and echoes it. But does not echo the previous one. Should have echoed the previous one as position zero and current one as [position 1.

And likewise should behave when everytime I refresh the page. The current password aswell as the previously saved ones in array should be echoed. They are not.

Issue remains.

<i>
</i>//SAMPLE 5D
//Ginerjm Sample.

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);
if(isset($_SESSION['secured_passwords_array']))
$_SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256',$password.$salts[$rand]);
echo "Current hashed salted password: $secured_password&lt;br&gt;";
$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
echo 'All secured passwords submitted: ';
echo "&lt;pre&gt;"; print_r($_SESSION['secured_passwords_array'],true); "&lt;/pre&gt;";
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — That is not my latest version. please make the changes I already pointed out.

$salts = array('121','gulf','¬',';@/');<br/>
$salts_count = count($salts)-1;<br/>
$rand = rand(0,$salts_count);<br/>
if(!isset($<EM>_SESSION['secured_passwords_array']))<br/>
$_</EM>SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256',$password.$salts[$rand]);<br/>
echo "Current hashed salted password: $secured_password&lt;br&gt;";

$<EM>_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.<br/>
echo 'All secured passwords submitted: ';<br/>
echo "&lt;pre&gt;" . print_r($_</EM>SESSION['secured_passwords_array'],true) . "&lt;/pre&gt;";<br/>
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645786

You had an extra opening bracket:
<i>
</i>echo "&lt;pre&gt;", print_r(($SESSION['secured_passwords_array'],true), "&lt;/pre&gt;";
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm

With all due changes as you said.
<i>
</i>//SAMPLE 5D
//Ginerjm Sample.

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);
if(!isset($_SESSION['secured_passwords_array']))
$_SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256',$password.$salts[$rand]);
echo "Current hashed salted password: $secured_password&lt;br&gt;";
$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
echo 'All secured passwords submitted: ';
echo "&lt;pre&gt;"; print_r($_SESSION['secured_passwords_array'],true); "&lt;/pre&gt;";


At first page load, it generates a password and dumps it to array and echoes it.

Then when I refresh the page and a 2nd password gets generated, then that gets dumped to the array and echoes it. But does not echo the previously array dumped one. On this round, should have echoed the previous one as position zero and current one as position 1.

And likewise should behave when everytime I refresh the page. The current password aswell as the previously saved ones in array should be echoed. They are not.

Issue remains.

I did try with:
<i>
</i>$_SESSION['secured_passwords_array'][]

But issue remained and so I then got rid of the [] from my own codes. But not from your's.

Still not working.

Test your code on your localhost.

Your code and all my variant codes, they never echo past the zero positioned password. Talking about the array data.
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — You need to have the [] on the new element assignation.

Here is my code once again. Works perfectly for me.

session_start();<br/>
$salts = array('121','gulf','¬',';@/');<br/>
$salts_count = count($salts)-1;<br/>
$rand = rand(0,$salts_count);<br/>
if(!isset($<EM>_SESSION['secured_passwords_array']))<br/>
$_</EM>SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256',$password.$salts[$rand]);<br/>
echo "Current hashed salted password: $secured_password&lt;br&gt;";

$<EM>_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.<br/>
echo 'All secured passwords submitted: ';<br/>
echo "&lt;pre&gt;" . print_r($_</EM>SESSION['secured_passwords_array'],true) . "&lt;/pre&gt;";<br/>
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @sempervivum,

Can you chime in and see why this code fails to echo previously saved passwords in the array ?

Run the code on your localhost.

Then refresh the page a few times.

You will see on each page refresh, only the currently saved (onto array) password gets echoed. And does not echo the previously saved passwords. Why is that ?

Here is my latest code:
<i>
</i>//SAMPLE 5A
session_start();

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);

if(EMPTY($_SESSION['secured_passwords_array']))
{
echo 'Line: ' .__LINE__ .'&lt;br&gt;';
$secured_passwords_array = array();
}

$secured_passwords_array = array();

echo 'Current hashed salted password: '.$secured_password = hash('sha256',$password.$salts[$rand]).'&lt;br&gt;';
echo 'Line: ' .__LINE__ .'&lt;br&gt;';

$_SESSION['secured_passwords_array'] = $secured_passwords_array[] = $secured_password; //Dump current secured password to array.

echo 'All secured passwords submitted: ';
$secured_passwords_array = $_SESSION['secured_passwords_array'];
var_dump($secured_passwords_array).'&lt;br&gt;';


And here is Ginerjm's code which is also behaving like my code above:
<i>
</i>//SAMPLE 5D
//Ginerjm Sample.

$password = 'password';
$salts = array('121','gulf','¬',';@/');
$salts_count = count($salts)-1;
$rand = rand(0,$salts_count);
if(!isset($_SESSION['secured_passwords_array']))
$_SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256',$password.$salts[$rand]);
echo "Current hashed salted password: $secured_password&lt;br&gt;";
$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
echo 'All secured passwords submitted: ';
echo "&lt;pre&gt;"; print_r($_SESSION['secured_passwords_array'],true); "&lt;/pre&gt;";
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Ignore
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645793

Mmm ?
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @nogdog

One of our codes should have worked but they ain't.

Now it has become a mystery!

So, let's try making the mystery a thing of history! The past!

How to solve this irritating issue ?

https://forum.webdeveloper.com/d/400361-why-array-leaks-memory-on-every-page-reload/43
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Ok here is a simple complete script that does your silly exercise perfectly. Run it AS IS!


session_start();

$salts = array('121', 'gulf', '¬', ';@/');

$salts_count = count($salts) - 1;

$rand = rand(0, $salts_count);

if(!isset($_SESSION['secured_passwords_array']))

$_
SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256', $password.$salts[$rand]);

echo "Current hashed salted password: $secured_password<br>";

$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.

echo 'All secured passwords submitted: ';

echo "<pre>" . print_r($_
SESSION['secured_passwords_array'],true) . "</pre>";

exit();

The only thing missing is the opening php tag. Add that and upload it and run it. If it fails I would like to see the complete script that you actually uploaded and ran because something is interfering it that happens and I want to see what you did.

Here is the output I get:

Current hashed salted password: 682e9763d43140d8f6c0fd71b2306482d1da550f6dd908b84178d11ecb380dcd

All secured passwords submitted:

Array

(

[0] => 1962cfc8a879092ecca66cbccb46aa2e4d94b21be98f3994b11fc0a4e3fffe4b

[1] => 682e9763d43140d8f6c0fd71b2306482d1da550f6dd908b84178d11ecb380dcd

[2] => 89aa1e580023722db67646e8149eb246c748e180e34a1cf679ab0b41a416d904

[3] => 6476f1c68b10d700112e2b8171b54df78a7adffa8f8213045c5a3f9f956a10be

[4] => 6476f1c68b10d700112e2b8171b54df78a7adffa8f8213045c5a3f9f956a10be

[5] => 6476f1c68b10d700112e2b8171b54df78a7adffa8f8213045c5a3f9f956a10be

[6] => 682e9763d43140d8f6c0fd71b2306482d1da550f6dd908b84178d11ecb380dcd

)
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645796

Ok. I now erased all my other attempted codes from my file.

Now just got your code.

I get this echoed on first page load:

**Warning: Undefined variable $password in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 11

Current hashed salted password: 89aa1e580023722db67646e8149eb246c748e180e34a1cf679ab0b41a416d904

Fatal error: Uncaught Error: [] operator not supported for strings in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php:14 Stack trace: #0 {main} thrown in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 14**

On 2nd page load or refresh, I get this echoed:

**Warning: Undefined variable $password in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 11

Current hashed salted password: 89aa1e580023722db67646e8149eb246c748e180e34a1cf679ab0b41a416d904

Fatal error: Uncaught Error: [] operator not supported for strings in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php:14 Stack trace: #0 {main} thrown in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 14**


On 3rd page load or refresh, I get this echoed:

**Warning: Undefined variable $password in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 11

Current hashed salted password: f154d4dd8f0a5d17d5bed20cbc18071020c9f10a8c40f41b45dba04026d0a4d2

Fatal error: Uncaught Error: [] operator not supported for strings in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php:14 Stack trace: #0 {main} thrown in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 14**

Here is the code.

Note, this forum might erase the underscore after the dollar sign:
<i>
</i>
session_start();
$salts = array('121', 'gulf', '¬', ';@/');
$salts_count = count($salts) - 1;
$rand = rand(0, $salts_count);
if(!isset($_SESSION['secured_passwords_array']))
$_SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256', $password.$salts[$rand]);
echo "Current hashed salted password: $secured_password&lt;br&gt;";

$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
echo 'All secured passwords submitted: ';
echo "&lt;pre&gt;" . print_r($_SESSION['secured_passwords_array'],true) . "&lt;/pre&gt;";
exit();


I erased the php opening & closing tags above as forum is rejecting my post unless I erase them. But they do exist in my file.

How is your script and my latest one behaving on your test on your localhost ?

Something is really interfering and erasing the previous saved data from the array on every page load. And only shows the saved data on the current run (current page load).

Feel like throttling php and asking when it is gonna quit it's game.
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — Try this

session_start();

error_reporting(E_ALL);

ini_set('display_errors', '1');

$password = 'password';

$salts = array('121', 'gulf', '¬', ';@/');

$salts_count = count($salts) - 1;

$rand = rand(0, $salts_count);

if(!isset($_SESSION['secured_passwords_array']))

$_
SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256', $password.$salts[$rand]);

echo "Current hashed salted password: $secured_password<br>";

$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.

echo 'All secured passwords submitted: ';

echo "<pre>" . print_r($_
SESSION['secured_passwords_array'],true) . "</pre>";

exit();
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645798

On every page reload, I get error like this:

Current hashed salted password: 1962cfc8a879092ecca66cbccb46aa2e4d94b21be98f3994b11fc0a4e3fffe4b

**Fatal error: Uncaught Error: [] operator not supported for strings in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php:17 Stack trace: #0 {main} thrown in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 17**

Line 17:
<i>
</i>$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.


Full your code tested:
<i>
</i>session_start();
error_reporting(E_ALL);
ini_set('display_errors', '1');
$password = 'password';
$salts = array('121', 'gulf', '¬', ';@/');
$salts_count = count($salts) - 1;
$rand = rand(0, $salts_count);
if(!isset($_SESSION['secured_passwords_array']))
$_SESSION['secured_passwords_array'] = array();

$secured_password = hash('sha256', $password.$salts[$rand]);
echo "Current hashed salted password: $secured_password&lt;br&gt;";

$_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
echo 'All secured passwords submitted: ';
echo "&lt;pre&gt;" . print_r($_SESSION['secured_passwords_array'],true) . "&lt;/pre&gt;";
exit();
Copy linkTweet thisAlerts:
@ginerjmAug 10.2022 — What version of php are you running?
Copy linkTweet thisAlerts:
@novice2022authorAug 10.2022 — @ginerjm#1645800

  • + Apache 2.4.52

  • + MariaDB 10.4.22

  • + PHP 8.1.2 (VS16 X86 64bit thread safe) + PEAR

  • + phpMyAdmin 5.1.1

  • + OpenSSL 1.1.0g

  • + ADOdb 518a

  • + Mercury Mail Transport System v4.63 (not included in the portable version)

  • + FileZilla FTP Server 0.9.41 (not included in the portable version)

  • + Webalizer 2.23-04 (not included in the portable version)

  • + Strawberry Perl 5.32.1.1 Portable

  • + Tomcat 8.5.73

  • + XAMPP Control Panel Version 3.3.0.

  • + XAMPP mailToDisk 1.0 (write emails via PHP on local disk in <xampp>mailoutput. Activated in the php.ini as mail default.)
  • Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — The message about the brackets on a string are cause you still have an old session running that has a string instead of an array. Shut down your session for a few minutes and start over.
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645802

    I retarted my browser a few times and that usually kills the session.

    Last night destroyed sessions too.

    Anyway, will try again now.

    I have put this on the top of the page: few mins ago
    <i>
    </i>ini_set('display_errors',1);
    ini_set('display_startup_errors',1);
    error_reporting(E_ALL);
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — You may have to give it a couple minutes of rest.
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645804

    I added this at the top of page:
    <i>
    </i>session_start();
    session_destroy();
    die;

    Then closed the firefox.

    Then erased the above code's last two lines and saved the file.

    Then fire fox started up. I refreshed the page a few times and no luck. Same issue.

    Shutting down computer and trying.
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — Add this code after the session start line:

    if (isset($_GET['reset']))

    {

    echo "Line: ". __LINE__ . " Resetting array<br>";

    unset($_
    SESSION['secured_passwords_array']);

    }

    Then add this to the url you are using to run the script:

    ?reset

    That will get rid of the previous session array and start a new one.

    Drop that reset thing after the first run
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645806

    I did all that. But no luck.

    I suspect $_SESSION is not the issue but the array() somewhere.

    EDIT:

    I never got echoed:

    **Resetting array**
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — Show me the revised code. All of it. Did you get the message about the array being reset?
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645808

    Url was this:

    https://localhost/Work/gulf/Templates/password_TEMPLATE.php?reset

    I never got echoed:

    Resetting array

    <i>
    </i>session_start();

    if (isset($GET['reset']))
    {
    echo "Line: ". LINE . " Resetting array&lt;br&gt;";
    unset($SESSION['secured_passwords_array']);
    }

    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $password = 'password';
    $salts = array('121', 'gulf', '¬', ';@/');
    $salts_count = count($salts) - 1;
    $rand = rand(0, $salts_count);
    if(!isset($_SESSION['secured_passwords_array']))
    {
    $secured_passwords_array = array();
    }

    $secured_passwords_array[] = $secured_password = hash('sha256', $password.$salts[$rand]);
    echo "Current hashed salted password: $secured_password&lt;br&gt;";

    $_SESSION['secured_passwords_array'] = $secured_passwords_array; //Add new secured password to array.
    $secured_passwords_array = $_SESSION['secured_passwords_array'];
    echo 'All secured passwords submitted: ';
    echo "&lt;pre&gt;" . var_dump($secured_passwords_array) . "&lt;/pre&gt;";
    exit();
    Copy linkTweet thisAlerts:
    @NogDogAug 10.2022 — [code=php]
    // make sure it exists and is an array before trying to add elements:
    if(!isset($_SESSION['secured_passwords_array']) or !is_array($_SESSION['secured_passwords_array'])) {
    $_SESSION['secured_passwords_array'] = [];
    }
    [/code]
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — I get error:

    Fatal error: Uncaught Error: Undefined constant "LINE" in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php:48 Stack trace: #0 {main} thrown in C:xampphtdocsWorkgulfTemplatespassword_TEMPLATE.php on line 48

    After I fixed your code by adding underscore after dollar sign.

    Gonna edit further and re-test.

    EDIT:

    Your typo:
    <i>
    </i>echo "Line: ". LINE. " Resetting array&lt;br&gt;";


    Fixed to:
    <i>
    </i>echo "Line: ". ___LINE__ . " Resetting array&lt;br&gt;";
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — You don't show an underscore in the GET var either.

    Why doesn't the LINE constant work for you? That is not right.

    Please post the code you are using for this test
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645808

    Ok.After fixing your code that the forum messes, I retested and this time got echoed about the rest. Then I commented out the line for reset to occur and refreshed the page a few times and yet still no luck.

    Now gonna try NpogDopg's code.
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — Those weren't typos. They are the fault of this forum's software that doesn't like certain things in our code and changes them. Wish they would fix it.
    Copy linkTweet thisAlerts:
    @NogDogAug 10.2022 — > @ginerjm#1645812 Why doesn't the LINE constant work for you?

    Because they used LINE instead of __LINE__.
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — Show me the code that you ran for me. If you get distracted by someone's else's suggestions I give up.
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645814

    EDIT:

    Tested code:
    <i>
    </i>session_start();
    /*
    if (isset($_GET['reset']))
    {
    echo "Line: ". __LINE__ . " Resetting array&lt;br&gt;";
    unset($_SESSION['secured_passwords_array']);
    }
    */
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $password = 'password';
    $salts = array('121', 'gulf', '¬', ';@/');
    $salts_count = count($salts) - 1;
    $rand = rand(0, $salts_count);
    if(!isset($_SESSION['secured_passwords_array']))
    {
    $secured_passwords_array = array();
    }

    $secured_passwords_array[] = $secured_password = hash('sha256', $password.$salts[$rand]);
    echo "Current hashed salted password: $secured_password&lt;br&gt;";

    $_SESSION['secured_passwords_array'] = $secured_passwords_array; //Add new secured password to array.
    $secured_passwords_array = $_SESSION['secured_passwords_array'];
    echo 'All secured passwords submitted: ';
    echo "&lt;pre&gt;" . var_dump($secured_passwords_array) . "&lt;/pre&gt;";
    exit();


    https://localhost/Work/gulf/Templates/password_TEMPLATE.php?reset

    No luck after getting echo about reset.

    Notice I later commnted-out the reset command line and refreshed page a few times but same issue.
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — YOU CHANGED MY CODE!
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @NogDog#1645810

    Where do I add this line NogDog ?
    <i>
    </i>// make sure it exists and is an array before trying to add elements:
    if(!isset($_SESSION['secured_passwords_array']) or !is_array($_SESSION['secured_passwords_array'])) {
    $_SESSION['secured_passwords_array'] = [];
    }[code]

    Here is the code ...
    [code]
    session_start();
    /*
    if (isset($_GET['reset']))
    {
    echo "Line: ". __LINE__ . " Resetting array&lt;br&gt;";
    unset($_SESSION['secured_passwords_array']);
    }
    */
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $password = 'password';
    $salts = array('121', 'gulf', '¬', ';@/');
    $salts_count = count($salts) - 1;
    $rand = rand(0, $salts_count);
    if(!isset($_SESSION['secured_passwords_array']))
    {
    $secured_passwords_array = array();
    }

    // make sure it exists and is an array before trying to add elements:
    if(!isset($_SESSION['secured_passwords_array']) or !is_array($_SESSION['secured_passwords_array'])) {
    $_SESSION['secured_passwords_array'] = [];
    }

    $secured_passwords_array[] = $secured_password = hash('sha256', $password.$salts[$rand]);
    echo "Current hashed salted password: $secured_password&lt;br&gt;";

    $_SESSION['secured_passwords_array'] = $secured_passwords_array; //Add new secured password to array.
    $secured_passwords_array = $_SESSION['secured_passwords_array'];
    echo 'All secured passwords submitted: ';
    echo "&lt;pre&gt;" . var_dump($secured_passwords_array) . "&lt;/pre&gt;";
    exit();
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645818

    Where ?
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645818

    You right!

    I created another version of your code and when you told me to add resetting code, I mistakenly added to the modified version.

    Now I added to your original version. And guess what ?YEEEEEHAAAA!

    It is working.

    Look:
    <i>
    </i>session_start();
    if (isset($_GET['reset']))
    {
    echo "Line: ". __LINE__ . " Resetting array&lt;br&gt;";
    unset($_SESSION['secured_passwords_array']);
    }
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $password = 'password';
    $salts = array('121', 'gulf', '¬', ';@/');
    $salts_count = count($salts) - 1;
    $rand = rand(0, $salts_count);
    if(!isset($_SESSION['secured_passwords_array']))
    $_SESSION['secured_passwords_array'] = array();

    $secured_password = hash('sha256', $password.$salts[$rand]);
    echo "Current hashed salted password: $secured_password&lt;br&gt;";

    $_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
    echo 'All secured passwords submitted: ';
    echo "&lt;pre&gt;" . print_r($_SESSION['secured_passwords_array'],true) . "&lt;/pre&gt;";
    exit();


    **Current hashed salted password: 22364b4382a3c1d8ab96ae4ad39aabb0863b6d9d6d3deb360939f12642a9cd73

    All secured passwords submitted:

    Array

    (

    [0] => 22364b4382a3c1d8ab96ae4ad39aabb0863b6d9d6d3deb360939f12642a9cd73

    [1] => ac5492014d97393b72f260a2b60872e23bc5d15d601604eb1f10894fafb72214

    [2] => 22364b4382a3c1d8ab96ae4ad39aabb0863b6d9d6d3deb360939f12642a9cd73

    )**
    Copy linkTweet thisAlerts:
    @NogDogAug 10.2022 — > @novice2022#1645819 Where do I add this line NogDog

    somewhere after session_start() (and after the unset() command) and before you try to add elements to that session variable. The idea is to create that element as an array before you try to add array elements to it. (Or you could just add the initialization to the bit @ginerjm gave you for resetting it.)
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — This is what I rewrote for you to test with. It works just fine
    [code=php]
    session_start();
    // add ?reset to the url to start over. Then remove it.
    if (isset($_GET['reset']))
    {
    echo "Resetting array<br>";
    unset($_SESSION['secured_passwords_array']);
    }
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $password = 'password';
    $salts = array('121', 'gulf', '¬', ';@/');
    $salts_count = count($salts) - 1;
    $rand = rand(0, $salts_count);
    if(!isset($_SESSION['secured_passwords_array']))
    $_SESSION['secured_passwords_array'] = array();
    $secured_password = hash('sha256', $password.$salts[$rand]);
    echo "Current hashed salted password: $secured_password<br>";
    $_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
    echo 'All secured passwords submitted: ';
    echo "<pre>" . print_r($_SESSION['secured_passwords_array'],true) . "</pre>";
    exit();
    [/code]

    DON'T CHANGE ANYTHING EXCEPT TO ADD THE OPENING PHP TAG AND REPLACE MISSING UNDERSCORES
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — at LAST you finally did what I asked.

    Good bye
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm

    THANKS MAN!

    You made history tonight.

    In many forums you always were haughty towards me. You probably forgotten me as I had many usernames in past. Like:

    developer_web

    unique_idea_man

    Google them.

    But you forgot who I was, otherwise you never would have bothered helping me tonight.

    And, now look. You fixed my issue without me needing to pay you. Hurray! Yeehaw!

    One day, God WIlling, I might return the FAVOUR! 😃 😄 😀 😁
    Copy linkTweet thisAlerts:
    @NogDogAug 10.2022 — > @ginerjm#1645823 REPLACE MISSING UNDERSCORES

    Wrapped your code in ... tags so that's not an issue. ;)
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645824

    So what was really the issue ? Yes, I can still ask this.
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @NogDog#1645822

    Ginerjm code is working.

    Why was not it and my code failing before ?

    ISSUE WAS: Not saving the previous data to array on every page load.
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @NogDog#1645826

    Even adding code with opening & closing php brackets within the code brackets, the forum says YOU DO NOT HAVE PERMISSION TO DO THAT.

    Infact, I get this mssg popping up a few times whenever I try making a post. IRRITATING.

    Why do not they fix it ?
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @NogDog

    If this is dev mode code ...
    <i>
    </i>session_start();
    if (isset($_GET['reset']))
    {
    echo "Line: ". __LINE__ . " Resetting array&lt;br&gt;";
    unset($_SESSION['secured_passwords_array']);
    }
    error_reporting(E_ALL);
    ini_set('display_errors', '1');
    $password = 'password';
    $salts = array('121', 'gulf', '¬', ';@/');
    $salts_count = count($salts) - 1;
    $rand = rand(0, $salts_count);
    if(!isset($_SESSION['secured_passwords_array']))
    $_SESSION['secured_passwords_array'] = array();

    $secured_password = hash('sha256', $password.$salts[$rand]);
    echo "Current hashed salted password: $secured_password&lt;br&gt;";

    $_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.
    echo 'All secured passwords submitted: ';
    echo "&lt;pre&gt;" . print_r($_SESSION['secured_passwords_array'],true) . "&lt;/pre&gt;";
    exit();


    How would you edit it to make it PROD mode ?

    And where should I add your code ?
    <i>
    </i>// make sure it exists and is an array before trying to add elements:
    if(!isset($_SESSION['secured_passwords_array']) or !is_array($_SESSION['secured_passwords_array'])) {
    $_SESSION['secured_passwords_array'] = [];
    }
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — I have tried using CODE or code in brackets and it fails. I get the you cant do this message all the time too.
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — How would you edit it to make it PROD mode ?

    And where should I add your code ?

    To move it to prod remove the unnecessary echos and turn off error checking. And do not add that isset or isarray line of code. You already HAVE IT!

    And I am fully aware of who I am dealing with.
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645832

    You mean not add Nogdog's code ?
    <i>
    </i>// make sure it exists and is an array before trying to add elements:
    if(!isset($_SESSION['secured_passwords_array']) or !is_array($_SESSION['secured_passwords_array'])) {
    $_SESSION['secured_passwords_array'] = [];
    }

    It is working without it.

    EDIT:

    After you fixed your code, I tried fixing my own codes with your fix but no luck. Frankly, I aint bothered trying to figureout why and learn the error of my ways. Sick of this thread and moving on by memorising your code.

    Once again thanks.
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — Frankly I wasn't at all interested in anyone else's code. I was too busy trying to make sure that you were running the code I gave you what will all the forum-induced problems and a couple of errs on my part.
    Copy linkTweet thisAlerts:
    @novice2022authorAug 10.2022 — @ginerjm#1645834

    Ok.

    Now is it not ok for me to further shorten the code from this:
    <i>
    </i>$secured_password = hash('sha256', $password.$salts[$rand]);
    $_SESSION['secured_passwords_array'][] = $secured_password; //Add new secured password to array.

    to this:
    <i>
    </i>$_SESSION['secured_passwords_array'][] = $secured_password = hash('sha256', $password.$salts[$rand]);


    EDIT:

    Seems to be working.
    Copy linkTweet thisAlerts:
    @NogDogAug 10.2022 — > @ginerjm#1645831 I get the you cant do this message all the time too.

    Yeah, wish they'd just fix that here.

    Sometimes you get the message but you can still submit, sometimes you can't. 🤷 One thing it will definitely reject is the opening PHP tag. You can either just leave it out, or stick a hyphen in there with a note to remove it. Either option still sucks. 🤷 (again)
    <i>
    </i>&lt;-?php // remove the hypen
    Copy linkTweet thisAlerts:
    @ginerjmAug 10.2022 — If you do that whats the purpose of having $secured_password? And why do you worry so much about shortening code considering how much meaningless and useless you post here all the time?
    ×

    Success!

    Help @novice2022 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 3.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,

    tipper: Anonymous,
    tipped: article
    amount: 10 SATS,
    )...