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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts

    SESS ID wont work!

    Code:
    <?php 
    
    if (session_id()) {
    session_start();
    
    echo $_SESSION['user'];
    
    }
    
    else { $_SESSION['user'] = $_POST['usernamelog']; }
    
    ?>
    When I login, nothing happens. The login script is embedded in the index.php and has an include("header.php"); with the above code at the top. However, it will not create a random session variable, aka "?id=235235235". Can someone tell me what's wrong with the above code? Thanks! I am new to php and trying to make all the functions for a future website.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by SKY-ProToSs View Post
    Code:
    <?php 
    
    if (session_id()) {
    session_start();
    
    echo $_SESSION['user'];
    
    }
    
    else { $_SESSION['user'] = $_POST['usernamelog']; }
    
    ?>
    When I login, nothing happens. The login script is embedded in the index.php and has an include("header.php"); with the above code at the top. However, it will not create a random session variable, aka "?id=235235235". Can someone tell me what's wrong with the above code? Thanks! I am new to php and trying to make all the functions for a future website.
    session_start must be first thing in the page else there is no session_id.

    best regards

  • #3
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by SKY-ProToSs View Post
    Code:
    <?php 
    
    if (session_id()) {
    session_start();
    session_start() starts a new session or continues an existing session.

    If you want to check if a user is logged in, then what you can do is set a session variable like $_SESSION['mySessionVar'] = 'qwerty' when you establish the user's bona fides and then at the top of every page where the user needs to be logged in check if the session variable exists and if it has the correct value.

    PHP Code:
    <?php
    session_start
    ();
    if(!isset(
    $_SESSION['mySessionVar']) || $_SESSION['mySessionVar'] != 'qwerty') {
        echo 
    'You are not an authorised user';
        die();
    }
    ?>

  • #4
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Hi guys. I am wondering if it is the server I am using. I tried a lot of peoples methods and not a single one worked. I even applied a random string value to the session_id :/

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by SKY-ProToSs View Post
    Hi guys. I am wondering if it is the server I am using. I tried a lot of peoples methods and not a single one worked. I even applied a random string value to the session_id :/
    try this, example #1 from this link:

    http://www.php.net/manual/en/session.idpassing.php

    few things:
    - session_start() must be before the rest of the code
    - you need to define a variable in order to retrive it on the next request but you also need to check if exists and to not overwrite it's value before you use it.

    best regards

  • #6
    New Coder
    Join Date
    Mar 2011
    Location
    Texas
    Posts
    22
    Thanks
    0
    Thanked 1 Time in 1 Post
    use
    PHP Code:
    session_start(); 
    as the first line of php page

  • #7
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    <?php session_start(); 
    
    if (!isset($_SESSION['user'])) {
    
    $_SESSION['user'] = rand();
    
    }
    
    else { $_SESSION['user']; }
    
    echo $_SESSION['user'];
    
    ?>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
    <head>
    <title>This is our website</title>
    <link href="main.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
    
    <div id="header">
    
    </div>
    Above is my header.php. I have included this file into every other file on my ftp server. The newest problem is that all I can do is display a string value for the $_SESSION. It wont write to the url and I can't figure out why. If I could get just a little bit more help, I'd be very greatful. Cheers.

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    you can manipulate the values like any other variable in php, you can print it as you did or you can do something else. That means you get the value of $_SESSION from previous page, you do something with it and if you want you pass to the next page by assigning the value to the $_SESSION variable.

    take a look to the example #1:

    http://www.php.net/manual/en/session.idpassing.php

    you need to read the manual page for complex examples and details:

    http://www.php.net/manual/en/book.session.php

    and of course post on the forum but with more specific questions.

    best regards

  • #9
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    PHP Code:
    <?php session_start();
    Any output, including blank lines and spaces, can cause session_start()to fail.

    Try this, making sure you have a carriage-return directly after <?php

    PHP Code:
    <?php
    session_start
    ();
    ...
    ...

  • #10
    Regular Coder
    Join Date
    Apr 2006
    Posts
    111
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Thank you everyone~!

    I figured out the problem after a good hour or two of research yesterday.

    The problem was that I had the session id filled with a value, but I forgot to output it into the header. So I just had it load to the header by using it in my href like this:

    <a href="posts.php?{$_SESSION['user']}" >Posts</a>

    Also outputting it in my redirect.

    The login only runs the if, if you are not logged in so I used a header(Location;

    Thanks!


  •  

    Posting Permissions

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