2

I am trying to use sessions to store information to be able to access on other pages with PHP.

I have done this several times with MySQL and it works perfectly, however with PostGreSQL it doesn't seem to work anywhere other than on the page created.

Here is a very simple example I tried to make and this doesn't even work (if you swap the psql for mysql - the below code works):

index.php:

<?php
    include_once("connection.php");
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Index</title>
    </head>
    <body>
        <div class="container">
            <p>hello</p>
            <form method="post" action="">
                <input type="text" name="name">
                <input type="submit" name="submit" value="Submit">              
            </form>
        </div>
    </body>
</html>

test.php:

<?php
    include_once("connection.php");

    $username = $_SESSION['name'];
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Index</title>
    </head>
    <body>
        <div class="container">
            <?php echo '<p> Hello ' . $username .'</p>';
            ?>
        </div>
    </body>
</html>

Connection.php:

<?php

    $conn_data = ("host=cimteaching1 port=27005 dbname='enter name' user='enter username' password='enter password'");
    $dbconn = pg_connect($conn_data) or die("Cannot connect to db" . pg_last_error());



    if(isset($_POST['submit'])) {
         $name = $_POST['name'];
         session_start();
         $_SESSION['name'] = $name;
         header("Location: test.php");
    }
?>
RandomMath
  • 675
  • 15
  • 33
  • You have `session_start` before you include `connection.php`, but then have another `session_start` inside `connection.php`? This is messy... – jszobody Mar 16 '16 at 13:51
  • That was just added as an extra test. – RandomMath Mar 16 '16 at 13:54
  • What errors are you getting exactly? As far as I can tell `$_SESSION['name']` is only set when a form is submitted (`$_POST`) so `$username = $_SESSION['name']` could well be nothing at all.... and **why** are you only starting the session if there's `$_POST['submit']` data? – CD001 Mar 16 '16 at 13:56
  • I don't seem to be getting any errors it just simply won't display. The connection with database is all correct – RandomMath Mar 16 '16 at 14:02
  • Unless you're submitting `$_POST['name']` you *should* be getting a *NOTICE: undefined index* error on test.php - and since index.php is posting to itself... Make sure you've got all error reporting turned on - I'm pretty sure that will tell you exactly where the problem is :http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display – CD001 Mar 16 '16 at 14:14

0 Answers0