1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Session problem Notice: Undefined index:

Discussion in 'PHP' started by moath11, Jul 28, 2014.

  1. #1
    Hi guys,
    when switched to the newer version of php
    i got some problems with sessions

    the scenario is
    file: login.php

    <?php
    session_start();

    $_SESSION['Username'] = $Username = $_POST['login'];
    $_SESSION['Password'] = $Password = $_POST['password'];
    header("Location: proccess.php");
    ?>


    now some another page

    file: parse.php

    <?php
    session_start();
    $Username = $_SESSION['Username'];
    $Password = $_SESSION['Password'];

    echo $Username;
    echo "\n";
    echo $Username;

    ?>



    now the problem is in parse.php when it's executed

    Notice: Undefined index: Username in C:\xampp\htdocs\clients\parse.php on line 3

    Notice: Undefined index: Password in C:\xampp\htdocs\clients\parse.php on line 4


    can anyone help me plz
     
    moath11, Jul 28, 2014 IP
  2. moath11

    moath11 Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #2
    fixed

    the problem was because the <?
    it should be <?php
     
    moath11, Jul 28, 2014 IP
  3. Krellen

    Krellen Greenhorn

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    18
    #3
    You are getting that error most likely because the $_SESSION['Username'] variable/index doesn't exist in the session array. To avoid that error/notice, you'd want to check for that variable before using it.

    For example:
    
    if (!empty($_SESSION['Username'])) {
        $Username = $_SESSION['Username'];
    }
    
    Code (markup):
    Or if you are using a new version of PHP, you could do something like this when assigning variables:

    
    $Username = $_SESSION['Username'] ?: null;
    $Password = $_SESSION['Password'] ?: null;
    
    Code (markup):
    The same method could be used in your login.php script when accessing the $_POST variables.
     
    Krellen, Sep 10, 2014 IP
  4. Vibrantu

    Vibrantu Greenhorn

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #4
    You can use even isset function to check whether session variable is set or not

    if(isset($_SESSION['Username']))
    {
    echo $_SESSION['username'];
    }
    Code (markup):
     
    Vibrantu, Sep 11, 2014 IP