Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:21:22+00:00 2026-05-31T08:21:22+00:00

Ladies and gents, I found a very strange behavior which I cannot explain: Assume

  • 0

Ladies and gents,

I found a very strange behavior which I cannot explain:

Assume that you have

  • multiple form elements on your page, maybe rendered by php
  • each form has one input field with an unique name
  • on the beginning of that page a session will be started
  • you store every posted input value in the $_SESSION variable

like this:

<?php
  session_start();
  $_SESSION["Test"] = "Hello";
  foreach ($_POST as $name => $value) {
    //echo "_POST: " . $name . ":" . $value . "<br>";
    $_SESSION[$name] = $value;
    //session_commit();
  }
  for ($i = 0; $i < 10; $i++) {
    echo "<form action=\"multiform.php\" method=\"post\">Value for input $i: <input type=\"text\" name=\"input".$i."\"></form>\n";
  }
  print_r($_SESSION);
?>

If you use the above code, only the “Test” = “Hallo” will persist after the refresh of the page. Regardless which input value has been posted and stored into the session by the foreach, it will be gone after refresh.

Now the interesting part:

If you add a name to the form like this…

echo "<form name=\"form$i\" action=\"multiform.php\" method=\"post\">Value for input $i: <input type=\"text\" name=\"input".$i."\"></form>\n";

…the posted values will be stored then.

But why?
What has the form name to do with the persistence of the $_SESSION?

EDIT: If the input name only contains numbers, the problem seems to arraise:

<input type=\"text\" name=\"$i\">

Thanks for clarifyng this.
Jan

EDIT2:

If the accessor key for the $_SESSION array only contains numbers, php obviously does not persist the values, so something like this, won’t be stored:

<?php
session_start();

for ($i = 1; $i < 10; $i++)
{
  $_SESSION[$i] = "Hello $i";
}
?>

The confusing part is, if you do a

print_r($_SESSION)

just after the for loop, it will show 1-10 with Hello 1..10…
Though, after refresh it’s gone…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:21:23+00:00Added an answer on May 31, 2026 at 8:21 am

    The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore.

    Found at http://php.net/manual/en/session.examples.basic.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ladies and gents, I have a CRM page which references 3 js files, (1x
Good evening ladies and gentlemen, I have a problem with Java Swing that I
Ladies and Gents, My best friends and I do a Secret Santa type gift
Hello again ladies and gents! OK, following on from my other question on ASP.NET
Hello ladies and gents, I'm having a bit of problem with accept() . I
Good afternoon ladies and gents. So, it is not my day for errors. Implementing
Ladies and gentlemen, I am working hard on a new webapplication which is based
OK Ladies and Gents, I would really appreciate some help: This is my table:
ladies and gentlemen! Very often on my job I meet the following requirement from
I have a stored procedure that generates a list of product sequence numbers. exec

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.