PHP Session help required

Discussion in 'PHP' started by pankajbains, Jan 22, 2008.

  1. #1
    I'm facing some problem in registering sessions in php. After login if I validate a session exist or not it show me it doesn't exist and take me back to the login sceen..

    I don't have access to .htaccess file so can't change the global veriables.
     
    pankajbains, Jan 22, 2008 IP
  2. greatlogix

    greatlogix Active Member

    Messages:
    664
    Likes Received:
    13
    Best Answers:
    1
    Trophy Points:
    85
    #2
    greatlogix, Jan 22, 2008 IP
  3. pankajbains

    pankajbains Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yes I'm using session_start(); on all files
     
    pankajbains, Jan 23, 2008 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Can you post your code?
     
    nico_swd, Jan 23, 2008 IP
  5. pankajbains

    pankajbains Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    here is code for login page

    if(isset($_POST['submit']))
    {
    $admin_name=$_REQUEST['adminname'];
    $npassword=$_REQUEST['password'];
    $password=md5($npassword);
    $query="SELECT * FROM admin where admin_name='$admin_name' and password='$npassword'";
    $result=mysql_query($query);
    $row=mysql_fetch_assoc($result);
    $num=mysql_num_rows($result);
    if($num>='1')
    {
    session_register('admin_name');
    session_register('id');
    header("location: main_page.php");
    }
    else
    {
    $msg="<B>Invalid Admin name and pssoword</B>";
    }
    }

    its working fine and goes to main_page.php... but don't find any session registered there and come back to this page. below is code for main_page.php

    session_start();
    $uid=$HTTP_SESSION_VARS['admin_name'];
    if($uid=="")
    {
    header("location:index.php");
    }
    else{-------code----}

    here it goes back to index page


    any suggestions ??
     
    pankajbains, Jan 23, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Don't use session_register(), it's deprecated. Just like $HTTP_SESSION_VARS.

    Use $_SESSION and to assign a new session do:
    
    $_SESSION['foo'] = 'bar';
    
    PHP:
    Then in the next page you can use it just like any other variable.
    
    echo $_SESSION['foo'];
    
    PHP:
    Also, have a look at www.php.net/mysql_real_escape_string - because your script allows SQL injection.
     
    nico_swd, Jan 23, 2008 IP
  7. pankajbains

    pankajbains Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hi session is working now but got new problem with copy file

    at local its working perfect but online giving permission error

    here is code

    mkdir('$var, '0777');
    copy('page',$var.'/page.php');

    can anyone help ?
     
    pankajbains, Jan 25, 2008 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    
    
    mkdir($var, 0777);
    copy('page.php' , $var . '/page.php');
    
    PHP:
    Like this?
     
    nico_swd, Jan 25, 2008 IP
  9. pankajbains

    pankajbains Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yes ,

    also I got the error in session variable again while using

    $_SESSION['foo'] = 'bar';
     
    pankajbains, Jan 25, 2008 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    Please include as much details as possible. I can't do anything with "I got an error".

    Are you using session_start(); in all pages? If so, what error? What exactly happens?
     
    nico_swd, Jan 25, 2008 IP
  11. pankajbains

    pankajbains Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    this code is for account.php file where user goes after login

    <?session_start();
    include("../includes/config.php");
    //echo $email=$HTTP_SESSION_VARS['email'];
    echo $email=$_SESSION['first_name'];
    include('../includes/header.php');

    ?>

    and this is login page code

    <?session_start();
    include("../includes/config.php");

    if($_POST['submit']||$_POST['submitmain'])
    {
    $email=$_POST['emaillogin'];
    $npassword=$_POST['password'];
    $password=md5($npassword);
    $queryl="SELECT * FROM `user-register` where `email`='$email' and `password`='$password' and status='1'";
    $resultl=mysql_query($queryl);
    $rowl=mysql_fetch_row($resultl);
    $regid=$rowl['regid'];
    $email=$rowl['email'];
    $first_name=$rowl['first_name'];
    $last_name=$rowl['last_name'];

    $num=mysql_num_rows($resultl);
    if($num>=1)
    {
    ini_set('session.use_cookies',false);
    $_SESSION['first_name'] = $first_name;
    session_register('email');
    session_register('first_name');
    session_register('last_name');
    session_register('regid');
    header("location:account.php");
    }

    else
    {
    header("location:login.php?msg=1");
    }

    }

    include('../includes/header.php');
    ?>

    It just take me to the account page with no error but not showing the username or session registered which i try to print.
     
    pankajbains, Jan 25, 2008 IP
  12. sharqi

    sharqi Guest

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #12
    
    <?
    session_start();
    //account.php
    print_r($_SESSION); //dumps the session array www.php.net\print-r
    echo "<hr>";  //echo a horizontal rule
    //lets print out the session data in a more readable format now
    foreach ($_SESSION as $key => $value) {  //www.php.net/foreach
    echo "Session Key:= " . $key . " - Session Value:= " . $value . "<br>";
    }
    ?>
    
    PHP:
    The best thing you can do with any script when it goes wrong is to output your data and take it from there.
     
    sharqi, Jan 25, 2008 IP
  13. pankajbains

    pankajbains Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    its showing me this

    Array ( => [last_name] => [regid] => [first_name] => ) Session Key:= emailSession Value:=
    Session Key:= last_nameSession Value:=
    Session Key:= regidSession Value:=
    Session Key:= first_nameSession Value:=

    no value in session
     
    pankajbains, Jan 25, 2008 IP
  14. pankajbains

    pankajbains Guest

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    ok Its working now got the solution thank guys sharqi and nico_swd thanks for your help
     
    pankajbains, Jan 25, 2008 IP
  15. sharqi

    sharqi Guest

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Anytime. Also you should post your fix then in the future people can search and use this thread for help.

    Also always remember to output things as soon as you hit an error you can not seem to fix. Saves much time and is good practise.
     
    sharqi, Jan 25, 2008 IP