Figuring out session variables? Having a problem

Discussion in 'PHP' started by scm22ri, Aug 24, 2012.

  1. #1
    Hi Everyone,

    I'm a little confused by sessions.

    This is my goal. In the below URL If someone clicks on "contact us" and they aren't logged in I want that user to be re-directed to a login page. I know I can do this with php sessions but I'm a confused as to how. Does the session have to be on the car-display-contact2.php or the car-display-contact-update.php? or both pages?

    How would you attack this problem?
    http://whatsmyowncarworth.com/auto-members/car-display/car-display-contact2.php

    Thanks everyone!
     
    scm22ri, Aug 24, 2012 IP
  2. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #2
    I am a PHP newbie myself, and hoping someone will give you a better response.
    I would like to inform you first: PHP sessions do not exist on files independently, the are stored in the server so, you can no chose of which files will have sessions because, all of your PHP files will have sessions once you start session.

    Anyway, if you want to make sure people are redirected to log-in page when they click ' Contact Us ' Then you should create a function called sessions:

    something like this:

    
     function session_holder(){
    
    if(isset($_SESSION['user_logedin']) $$ !empty($_SESSION['user_loggedin'])){
    return true; 
    }else{
    return false;
    }
    }
    
    
    
    Code (markup):


    Now on the ' Contact Us ' page,

    you can check if user is logged in or nor and redirect them while they press the submit button for the contact


    
    
    if(isset($_POST['SUBMIT']) && empty($_SESSION['user_logedin'])){
    
    header('Location:login_page.php')
    
    } else {
    
    // Put what you want php to do after 'Contcs Us' is pressed;
    
    
    
    }
    
    
    
    
    Code (markup):


    Like I said, I am very new to PHP and haven't tested this script, but you can get the logic behind your solution.. i hope.


    Good luck
     
    eritrea1, Aug 25, 2012 IP
  3. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #3
    Oh, I see :) Well, test it did it work for you? If not, let me know I will try to create the script, test it and then post it here.
     
    eritrea1, Aug 25, 2012 IP
  4. scm22ri

    scm22ri Greenhorn

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #4
    Hey Everyone,


    I made a little progress today. I added the below syntax and a login URL link is presented. Now I'm trying to figure out after a user logs in how would I get that user back to the car-display-contact-update.php? page? What variables would I need to carry from page to page?

    How would I get the visitor back to the below page after they login?
    http://whatsmyowncarworth.com/auto-...=57&make=Toyota&model=Camry&submit=Contact+Us

    This is the syntax I added on my car-display-contact-update.php page
    if (!isset($_SESSION['id'])) {    
    echo 'Please <a href="http://whatsmyowncarworth.com/auto-members/login.php">log in</a> to access your account';   
    } 
    
    PHP:
     
    Last edited: Aug 25, 2012
    scm22ri, Aug 25, 2012 IP
  5. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #5
    The only thing you need for transferring people from any page to any page is by using the header function. Which is:

     header('Location:[COLOR=#0000FF][FONT=monospace]http://whatsmyowncarworth.com/[/FONT][/COLOR][COLOR=#111111]car-display-contact-update.php[/COLOR] ') 
    Code (markup):
    This is after your client has logged in: I assume, you have already created the login script
     
    eritrea1, Aug 25, 2012 IP