SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    session not carrying through with href links

    Quick question. My sessions currently work on all my pages that use the a header redirect (i.e. ("Location: page.php")) however i have a portal page and want the session to carry over to these other pages which the user accesses by clicking the link. All the info is there just seems to not be carrying onto the new page.

    Still only new to PHP so I'm sure it's something simple/I don't know about but appreciate any help.

    I have code of what I have done if that helps but I've narrowed it down to that being the problem.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2008
    Posts
    120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check if source URL and destination URL are in same domain name. If domain is different session is stored in coocie and browser does not send it to the server.

  3. #3
    SitePoint Zealot cholmon's Avatar
    Join Date
    Mar 2004
    Location
    SC
    Posts
    197
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure you're using start_session() on that portal page that you're linking to? If so, is it on the same domain?
    Drew C King: PHP Developer
    <?= $short_tags++ ?>

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To elaborate on "same domain"

    These are all considered different, although there is a way to instruct the browser that it should consider them the same for purposes of cookie handling.
    http://example.com
    http://www.example.com
    http://subdomain.example.com

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yep start_session() used. As for same domain yes, not uploaded and running on localhost in the same folder.

    Below is the code for the two pages.

    Manager Portal
    PHP Code:
    <?php
    $message 
    "";
    include(
    "functions.php");
    session_start();
    $user $_SESSION["username"];
    print
        (
    "
           <div id='titlebar'>
                <div id='nav'>
                    &nbsp; You are logged in as 
    $user &nbsp;
                </div>
           </div>
        "
    );
        if (!isset(
    $user))
        {
            
    header("Location: error.php");
        }
        else
        {    
        }
    ShowUsersGold($user);
    ?>

    <html>
    <head>
        <title>Manager Portal</title>
        <link href="main.css" rel="stylesheet" type="text/css">
        <style type="text/css">
            form {
                Height: 100px;
            }
        </style>
    </head>
    <body>
        <h1>Manager Portal</h1>
        <p><a href='login.php' title='Logout' onclick='<?php session_unregister('username'); ?> '>Logout</a></p>
        <form>
            <a href="add.php">Add Users</a><br/>
            <a href="editm.php">Edit Users</a><br/>
            <a href="delete.php">Delete Users</a><br/>    
        </form>
    </body>
    </html>
    page linking from manager portal
    PHP Code:
    <?php
    $message 
    "";
    include(
    "functions.php");
    session_start();
    $user $_SESSION["username"];
    print
        (
    "
           <div id='titlebar'>
                <div id='nav'>
                    &nbsp; You are logged in as 
    $user &nbsp;
                </div>
           </div>
        "
    );

            if(isset(
    $_POST["usernameFind"]))
            {
                
    $usernameuse $_POST["usernameFind"];
                
    $exists UsernameExists($usernameuse);
                if(
    $exists == 1)
                {
                    
    DeleteUser($usernameuse);
                    print(
    "<br/>
                            <div id='found'>
                                
    $usernameuse found.<br/>
                                
    $usernameuse has now been deleted.
                            </div>"
    );
                }
                elseif(
    $exists == 0)
                {
                    print(
    "<br/>
                            <div id='error'>
                                <b>ERROR:</b> 
    $usernameuse could not be found.
                            </div>"
    );
                }
            }
        
    ?>

    <html>
    <head>
        <title>Delete User</title>
        <link href="main.css" rel="stylesheet" type="text/css">
        <style type="text/css">
            form {
                Height: 100px;
            }
        </style>
    </head>
    <body>
        <h1>Delete User</h1>
        <p>Once you enter a correct username the system will delete that user.</p>
        <form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
            <p><a href='login.php' title='Logout' onclick='<?php session_unregister('username'); ?> '>Logout</a></p>
            <p>Username:</br> <input class=input type="text" name="usernameFind" size="20"></p>
            <p><input class=button type="submit" value="Submit" name=""></p>
        </form>
    </body>
    </html>
    Thanks, I'm quiet stumped and as I said still a newbie - I have tired a few different things before asking.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The header() function does not stop script execution. Your php script will continue to execute to the end. If you want to stop script execution, you need to use exit(). This is very important if you're trying to prevent certain pages from being seen by unauthorized users, or just prevent certain code from running under certain conditions.

    php is a serverside programming language. This means every bit of php code is executed on your server before the browser gets a chance to see the resulting html. If you want to see what your browser sees, simply right click>view html source. Your webserver goes through the entire php file and strips out all the php code by executing it. The result ends up being plain old html, and this plain old html is what is then sent to the browser. Understanding this server to client concept is very important http://www.sitepoint.com/article/adv...design-primer/



    Notice, that your html <a> link has an empty onclick attribute. session_unregister() does not output a string or value. You should also by now realize that session_unregister() was already executed , when your intention was to only execute it if the user clicked the link.

    If you want to make a logout link, just link to a logout.php file and see the example code here http://www.php.net/session_destroy

    btw- dont use session_register() or session_unregister(). These are old, ancient functions which aren't really used anymore, and wont exist much longer. If you come across a tutorial using them, hit the back button and go elsewhere

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'm aware of a lot of things you've mentioned. As I said only new to PHP (only 1 lesson a week) so I'm going on what I have learn't in class, read online and my other coding/programming knowledge.

    Yeah I was aware the logout was pretty dodgy as had trouble with it before. However I even took it out to see if the session would carry over but it didn't, I realise the logout would be easy enough to fix but it's the session carrying over which is the important part at the moment. Place an exit after the header but no luck.

    Like I said session carries over with all my other pages which are just redirects using header but not my href.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What does the current code look like now?

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The same, taken out the logout as I work on that later. Added the exit to the header on manager but session isn't carrying over.

  10. #10
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A good habit to get into is to turn error_reporting up when developing.
    PHP Code:
    // top of all scripts, or set these in your php.ini or .htaccess file
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    php will complain for a lot of common mistakes. See if you get anything.

    use var_dump() and print_r() liberally to inspect the contents of your variables and return values, to make sure they're really what you think they are, at key points in the script.

    Check the contents of the $_COOKIE array, because php sessions use a cookie to pass the session id around. The value should not be changing.

    Barring that...some of your user defined functions could potentially cause side effects.

  11. #11
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Already got that turned on. Hence knowing the error which is:

    Notice: Undefined index: username in C:\webserver\htdocs\discountclub\delete.php on line 5.

    Which like I said seems to not be carrying through a href links as opposed to header redirects.

  12. #12
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pritnep View Post
    Which like I said seems to not be carrying through a href links as opposed to header redirects.
    What do you mean by ."..not be carrying through a href links..."?? AFAIK, sessions are not supposed to be carried from URLs. I think you must have known to the fact that sessions are stored in the server and you don't have to carry any sessions variables from query string/URLs when going to next page. Just have a link to go to the next page and in the next page start the session and you can directly use all the session variables that are previously set in other pages within the session or so.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  13. #13
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't mean actually being carried with the link but like above I have a session started on the next page but for whatever reason it's not displaying the informat dispite it working on previous page (with same code) and other pages.

    The only difference being to get to these pages you have to click a link instead of header redirect.

  14. #14
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whatever you have used to go from one page to another, the sessions will be carried automatically in the next page. What are you trying to do with this?
    PHP Code:
    <a href='login.php' title='Logout' onclick='<?php session_unregister('username'); ?> '>Logout</a>
    See here how the login system normally works (my own way, others might do it differently)
    1. Have a login form in login.php file/page
    2. Process the login (check entered username and password against the database or whatever you have stored the username password) in loginprocess.php page
    loginprocess.php should look like this:
    PHP Code:
    session_start();
    if(
    login == success){
        
    // set session variables here
        
    $_SESSION['is_logged_in'] = true;
        
    $_SESSION['logged_in_username'] = 'enteredusername';
        
    //redirect to the private page
        
    header('Location: useraccount.php');
        exit();
    }
    else{
        
    header('Location: login.php');
        exit();

    4. in useraccount.php file
    PHP Code:
    session_start();
    echo 
    'Welcome ' $_SESSION['logged_in_username'];
    // or whatever HTML you have put here under.
    echo '<a href="nextpage.php">Go to next page</a>';
    echo 
    '<a href="logout.php">Logout</a>'
    5. in nextpage.php
    PHP Code:
    session_start();
    echo 
    'Welcome ' $_SESSION['logged_in_username']; // you will still have the username displayed here
    // or whatever HTML you have put here under.
    echo '<a href="useraccount.php">user account page</a>';
    echo 
    '<a href="logout.php">Logout</a>'
    6. logout.php
    PHP Code:
    session_start();
    session_destroy()
    // or simply you can unset the session variables if you dont want to destroy the whole session
    unset($_SESSION['is_logged_in'], $_SESSION['logged_in_username']); 
    I hope you understand and this will help you to find out the problem.
    Last edited by Raju Gautam; Jun 20, 2009 at 23:05.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  15. #15
    SitePoint Enthusiast
    Join Date
    Feb 2009
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's the thing it's working on every other page...

    Well since building this from stratch I didn't look at how our system did theirs/or the standard that was my attempt at a logout but that's been changed, now - and it wasn't what was causing the problem as I removed it.

    Still not carrying over, thought I worked out where just not why but seems to be escaping everyone.


Bookmarks

Posting Permissions

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