Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    2 issues with variables: header() and include()

    OK, trying to do things the right way here, haven't done much coding for a few years. Current issues are with my variables not being there when I need them. The first is with form validation and using header() to redirect, the $_SESSION variable is empty. The second is that the variables I define in a include() file are showing up empty also. Probably something simple, I never had to worry much about security before, so using session variables is a new endeavor.

    Here is the relevant code from the login page at index.php:

    PHP Code:
    <?
    session_start
    ();

        
        if (isset(
    $_SESSION['errormsg'])) {
            echo 
    $_SESSION['errormsg'];
    }
     
    ?>
    and the code from the authorization page:

    PHP Code:
    <?php 
    //VARS
    session_start();

    $uname $_POST['uname'];
    $upass $_POST['upass'];
    $action $_POST['action'];


    if(
    $uname == "") {
                
    $_SESSION['errormsg']='YOU MUST ENTER A USER NAME';
                
    header('Location: index.php');
                die();
    }
     
    if(
    $upass == "") {
                
    $_SESSION['errormsg']='PASSWORD REQUIRED';
                
    header('Location: index.php');
                die();
    }

     if(
    $action != "auth") {
                
    $_SESSION['errormsg']= 'ACCESSS DENIED';
                
    header('Location: index.php');
                die();
    }  else {
    if(
    $action == "auth") {
        include (
    "comms.php");
        
    /*//comms.php contains the following, if I uncomment this, the $link db test works
        $server = "localhost";
        $user = "root";
        $pass = "pass";
        $db = "db";*/

    //Just a test for db connection
        
    $link mysql_connect($server$user$pass);
    if (!
    $link) {
        die(
    'Could not connect: ' mysql_error());
    }
    echo 
    'Connected successfully';
    mysql_close($link);

        }}

    ?>
    When I do not include a $uname or $upass, I do get kicked back to the index.php, but the $_SESSION['errormsg'] does not display, and before I put the isset in there, I was getting an undefined variable error.

    Current errors:
    Notice: Undefined variable: server in auth.php on line 41

    Notice: Undefined variable: user in auth.php on line 41

    Notice: Undefined variable: pass in auth.php on line 41

    Warning: mysql_connect() [function.mysql-connect]: Access denied for user ''@'localhost' (using password: NO) in auth.php on line 41
    Could not connect: Access denied for user ''@'localhost' (using password: NO)


    I thought that when you used require or include, it was the same as having it right there in same page?

  • #2
    Regular Coder
    Join Date
    Jan 2011
    Posts
    120
    Thanks
    6
    Thanked 2 Times in 2 Posts
    You need to declare your database connection variables.

    PHP Code:
    $link mysql_connect($server$user$pass); 

  • #3
    Regular Coder
    Join Date
    Jan 2011
    Posts
    120
    Thanks
    6
    Thanked 2 Times in 2 Posts
    I would suggest you create a php database connectivity script and include it when you need it.

  • #4
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The included file (comms.php) contains the db variables as noted in the comments

    PHP Code:
    $server "localhost"
        
    $user "root"
        
    $pass "pass"
        
    $db "db"
    but php is not recognizing them- is there something more I need to do to declare them other than the code above?

  • #5
    Regular Coder
    Join Date
    Jan 2011
    Posts
    120
    Thanks
    6
    Thanked 2 Times in 2 Posts
    ahh, sorry about that. try changing include to require_once. Also make sure if it's in different directory that you are also including that in your require.

  • #6
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattClark View Post
    ahh, sorry about that. try changing include to require_once. Also make sure if it's in different directory that you are also including that in your require.
    Yeah, got the path right, and unfortunately, require_once didn't do the trick.

    Should I be using some sort of session or other variable array? Again, it's my understanding that an include or require should function just like it is in the code of the page that is being parsed.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by ballistic View Post
    The included file (comms.php) contains the db variables as noted in the comments

    PHP Code:
    $server "localhost"
        
    $user "root"
        
    $pass "pass"
        
    $db "db"
    but php is not recognizing them- is there something more I need to do to declare them other than the code above?
    One problem at a time.
    Is this all that is in the comm.php file? Require_once should be used for two reasons: the first is that the declaration is required to proceed, and the _once will prevent issues on multiple includes. That only affects altered variables or function/class definitions though.
    Path appears correct as the error would indicate a pathing problem prior to the variable errors.

    As for actually including them in the script, that's not incorrect, but it is partially inaccurate. The inclusion imports variables to the scope of the call. In an if branch, they will not exist beyond the if. According to this, this will not be the problem. I'm leaning towards an unset.

    As for the sessions themselves, have the sessions successfully established cookies, or are they passed via SID? If they are passed via SID in the querystring, headers will not add these back in. Attempt to pass a header by adding ?' . SID as a part of it. SID is only defined if cookies could not be set.

  • #8
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    One problem at a time.
    Is this all that is in the comm.php file?
    The entire contents:
    PHP Code:
    <?
        $server 
    "localhost";
        
    $user "root";
        
    $pass "2112";
        
    $db "sundance";
        
        
    $connection mysql_connect ($server$user$pass);
    ?>
    Quote Originally Posted by Fou-Lu View Post
    Require_once should be used for two reasons: the first is that the declaration is required to proceed, and the _once will prevent issues on multiple includes. That only affects altered variables or function/class definitions though.
    Path appears correct as the error would indicate a pathing problem prior to the variable errors.
    I will start using require_once.

    Quote Originally Posted by Fou-Lu View Post
    As for the sessions themselves, have the sessions successfully established cookies, or are they passed via SID? If they are passed via SID in the querystring, headers will not add these back in. Attempt to pass a header by adding ?' . SID as a part of it. SID is only defined if cookies could not be set.

    They are not passed via SID. I assume the cookies are working because I tried the favcolor/animal/time script in the php.net example for session_start();, and that worked, it just doesn't work when using the header() redirect.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You are using short tags. Does accessing the comm.php file directly show the PHP code within the HTML source?

  • #10
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    You are using short tags. Does accessing the comm.php file directly show the PHP code within the HTML source?
    it does- and in firefox the source code is green. Should I be using some other method?

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Yes you should be using <?php instead of <?

    <? is known as a short tag. It only works if php has short tags turned on in the php.ini file. Many hosts don't enable short tags.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    New Coder
    Join Date
    Mar 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Yes you should be using <?php instead of <?

    <? is known as a short tag. It only works if php has short tags turned on in the php.ini file. Many hosts don't enable short tags.
    You gotta be freakin kidding me! I was taught back in the day that it didn't really matter. That fixed both issues.

    THANK YOU. I'm sure I'll be back for more throughout this project.
    Last edited by ballistic; 11-19-2011 at 07:55 PM.

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,313
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Quote Originally Posted by ballistic View Post
    You gotta be freakin kidding me! I was taught back in the day that it didn't really matter.
    If short tags were enabled on your previous hosts and/or the host you were taught on then no, it wouldn't of mattered. Fact is that you can't rely on short tags being turned on though so best just to stick to <?php

    I also found out the hard way. I learnt to use <? and when uploading to my server hell broke loose
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •