Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "J.O. Aho" Newsgroups: comp.lang.php Subject: Re: php session variable is not passed Date: Thu, 29 Dec 2016 10:30:45 +0100 Lines: 167 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Trace: individual.net rI9WSuJz+xEoQd40zkRoUgSc6alvoDYKoCr7bm7MlV03InoODI Cancel-Lock: sha1:+KJP/c8TJlc0Q8FuIwlux/Ci9GI= X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.8.0 In-Reply-To: Xref: csiph.com comp.lang.php:17243 On 29.12.2016 02:39, . wrote: > I am facing problem with php sessions . Suppose I have two pages 1)form.php 2)receiver.php . I want to pass a variable frrom form.php to receiver.php . here is my code > > > > > form.php : > > > session_start(); > ?> > > > > > > >
>
You are telling that the page to where you post is receiver.php, so the ... > > required> > > >
>
> > if (isset($_POST["sb"])) > { code here is never executed ... > $u = $_POST["uF"]; > echo "u : " . $u . "
"; > $_SESSION["username"] = $_POST["uF"]; > > > echo "get : " . "
"; > print_r($_GET); > echo "
"; > echo "post : " . "
"; > print_r($_POST); > echo "
"; > echo "session : " . "
"; > print_r($_SESSION); > echo "
"; > > } > ?> > > > > > > receiver.php : > > session_start(); > > > $r =$_SESSION["username"]; so the session value username never been set > echo "r : "."
"; > echo $r; > echo "
"; > > echo "get : "."
"; > print_r($_GET); > echo "
"; > echo "post : "."
"; > print_r($_POST); > echo "
"; > echo "session : "."
"; > print_r($_SESSION); > echo "
"; > > > ?> There are two ways you can solve this, the simple way, just remove the PHP code in your form page --- form.php ---

--- EOF --- and in your receiver script you set the post data --- receiver.php --- "; $_SESSION["username"] = $_POST["uF"]; echo "get : " . "
\n"; print_r($_GET); echo "
\n"; echo "post : " . "
\n"; print_r($_POST); echo "
\n"; echo "session : " . "
\n"; print_r($_SESSION); echo "
\n"; echo "Please visit our welcome page.
\n"; } else { echo "Nothing was posted that we can use
\n"; } ?> --- EOF --- This is how things are traditionally done. When you have set the session data, then you can go to a third page and the script could look like this (keep in mind there are no error handling, than you should fix yourself) --- thirdpage.php --- \n"; ?> --- EOF --- The second way you could do this, but require you use buffering (no code this time), you would need to change the post of the form.php to be to form.php instead of receiver.php. After you set the session variable username you use header() to redirect to receiver.php. If you don't buffer your output, then there will be data sent before the header is sent, which will make the header() to fail and as it has great potential to fail for you, I don't provide any example. -- //Aho