简体   繁体   中英

Sessions in php not working as expected

I have a file "test.php" like this:

<?php
session_start();
unset($_SESSION['ph']);
require 'connection.php';
if(!session_id())
    session_start();
else
{
    echo session_id();
    echo "<br>";
    echo var_dump($_SESSION);
}
$ph = 0;
if(isset($_POST['ph']))
    if(isset($_POST['submit']))
    {
        $ph = $_POST['ph'];
        $q1 = "select PHONE_CUST_ID from PHONE where PHONE_NO = :ph_bv";
        $prepare = oci_parse($conn, $q1);
        oci_bind_by_name($prepare, ':ph_bv', $ph);
        oci_execute($prepare);
        $res = oci_fetch_array($prepare, OCI_ASSOC);
        if(!$res)
            header('Location:newcustomer.php');
        else
            header('Location:oldcustomer.php');
    }
?>
<!doctype html>
<html lang="en">
  <body>  
    <form method="POST" action="" id="custphoneform">
        <label for="PhoneNumber">Enter Phone Number:</label>
        <input type="number" name="ph" required>
        <input type="submit" value="Submit" name="submit">
    </form>
  </body>
</html>

The "oldcustomer.php" and "newcustomer.php" have similar code as below:

<?php
require 'connection.php';
session_start();
if(isset($_SESSION['ph']))
{
    echo $_SESSION['ph'];
}
else
    echo "Not found";
?>

The session variable is not working, gives me the following error:

Notice: Undefined index: ph in D:\xampp\htdocs\myfiles\newcustomer.php on line 4

The session works properly if I set the "form action" to some value but I don't want that. I want redirection based on user input. How to achieve it?

Example:

"test.php"
 Enter Phone Number: 100

Based on the above value ie 100 I want to direct the user to different pages. If the number exists in the DB then oldcustomer.php otherwise newcustomer.php .

How can I do this?

You didn't give any value for action attribute here, So it will always post the data on current page only which is test.php . So you are getting error at oldcustomer.php when you are trying to access the variable.

 <form method="POST" action="" id="custphoneform">

either you can directly post your data to oldcustomer.php by specifying it in action attribute as

 <form method="POST" action="oldcustomer.php" id="custphoneform">

or you should store your data in $_SESSION global variable so you can access it anywhere in your project as

 $_SESSION['ph'] = $_POST['ph'];

use this variable in oldcustomer.php as

<?php
    session_start();
    if(isset($_SESSION['ph'])){
        echo $_SESSION['ph'];
    }else{
        echo "Phone number not found";
    }
?>

You are redirecting after form post. At that time new request will be initialize which doesn't contain post data. Whsat you can do is in test.php assign session which will then use in any page

test.php

<?php
session_start();
require 'connection.php';

$ph = 0;
if(isset($_POST['ph']))
    if(isset($_POST['submit']))
    {
        $ph = $_POST['ph'];
        $q1 = "select PHONE_CUST_ID from PHONE where PHONE_NO = :ph_bv";
        $prepare = oci_parse($conn, $q1);
        oci_bind_by_name($prepare, ':ph_bv', $ph);
        oci_execute($prepare);
        $res = oci_fetch_array($prepare, OCI_ASSOC);
        if(!empty($_POST['ph']))
        $_SESSION['ph'] = $_POST['ph']; //assign value to session
        if(!$res)
            header('Location:newcustomer.php');
        else
            header('Location:oldcustomer.php');
    }
?>

oldcustomer.php and newcustomer.php

<?php
session_start();
echo $_SESSION['ph'];
?>

you redirect to xxxcustomer.php (by the line header('Location: ...); . That will be a GET request.

So $_POST is empty.

So the if() will not be executed. { } are missing so the echo line will be executed next.

But no $_SESSION['ph'] is set >>> warning missing index ph

edit I suppose you better do an include() on those xxxcustomer.php files?

EDIT 2

As you changed your code in the question, this answer makes no sense anymore. Bottom line remains: after a redirect you loose the values in $_POST as it no longer is a POST-request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM