SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 49
  1. #1
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking for sessions

    hi all,
    i have written a code for checking whether there is a session or not.
    if not it has to be replaced with the time() value.
    but it is displaying the error as Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\shopping1\session.php:9) in C:\xampp\htdocs\shopping1\session.php on line 27

    below is my code.....
    PHP Code:
    <?php 
    session_start
    ();
    mysql_connect("localhost","root","") or die("mysql_error()");
    mysql_select_db("shopping") or die("mysql_error()"); 
    $result mysql_query("SELECT * from login WHERE username='" $_POST['username'] . "' AND password='" $_POST['password'] . "'"); 
    $rows=mysql_num_rows($result);
    if(isset(
    $_SESSION['username']))
    {
     echo 
    "UserId : ".$_SESSION['username'];
     unset(
    $_SESSION['username']);

    else 
    {
     echo 
    "Set the username";
     
    $_SESSION['username'] = time();
    }
    if (
    $rows 0

    session_register('username');
    $_SESSION['username'] = $_POST['username'];
    header("Location:products.php");
    exit; 

    else

    //unsuccessful login 
    header("Location:login.php");
    exit; 

    ?>
    what is the error in my code......

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    You're creating output before the header() lines. Get rid of the two echo's in the first IF, and that error should be gone.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    where should i get rid of the echo .

  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    I already told you.
    Just look at the code you posted. There are two echoes before the script reaches the header() instructions. You can't send headers if you already have output. So you'll have to eliminate those echoes.

  5. #5
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reply

    k i have got rid of the echo's but also not executing..
    below is my modified code.....
    PHP Code:
    <?php 
    session_start
    ();
    mysql_connect("localhost","root","") or die("mysql_error()");
    mysql_select_db("shopping") or die("mysql_error()"); 
    $result mysql_query("SELECT * from login WHERE username='" $_POST['username'] . "' AND password='" $_POST['password'] . "'"); 
    $rows=mysql_num_rows($result);
    if(isset(
    $_SESSION['username']))
    {
     
    "UserId : ".$_SESSION['username'];
     unset(
    $_SESSION['username']);

    else 
    {
     
    "Set the username";
     
    $_SESSION['username'] = time();
    }
    if (
    $rows 0

    session_register('username');
    $_SESSION['username'] = $_POST['username'];
    header("Location:products.php");
    exit; 

    else

    //unsuccessful login 
    header("Location:login3.php");
    exit; 

    ?>

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    When it's giving you an error, post it here, instead of saying "doesn't work" or "not executing".

    And please take a look at your code, and think about things just a moment before asking here. Isn't there anything strange now? Nothing that makes you think: that can't work?

  7. #7
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    first i need to register for username and using that i need to check for the sessions of username right...

  8. #8
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    first i need to ....
    First you need to study the basics of PHP.
    Tell me what is wrong in this code:
    PHP Code:
    if(isset($_SESSION['username']))
    {
     
    "UserId : ".$_SESSION['username'];
     unset(
    $_SESSION['username']);


  9. #9
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    we are checking whether we have set username and assigning username to userid and then destroying it.
    also userid is not a variable....we need to assign as $userid.

  10. #10
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    It's your code. I just took a little piece of it. And no, you're not assigning anything. This line
    PHP Code:
     "UserId : ".$_SESSION['username']; 
    makes no sense anymore because you deleted the word 'echo' that was in front of it.

  11. #11
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    userId is a not variable to store the name of the username right

  12. #12
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, you have put it in quotes which makes it a literal string. You need to do something with the literal string, it won't output if you just put it there in quotes.

  13. #13
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reply

    Quote Originally Posted by Stormrider View Post
    No, you have put it in quotes which makes it a literal string. You need to do something with the literal string, it won't output if you just put it there in quotes.
    i am not getting you what u are telling....

  14. #14
    Avid Logophile silver trophy
    ParkinT's Avatar
    Join Date
    May 2006
    Location
    Central Florida
    Posts
    2,343
    Mentioned
    192 Post(s)
    Tagged
    4 Thread(s)
    In PHP you will consistently receive errors if send any output (as with the 'echo' command) before executing the 'header' call.
    As I follow the FLOW OF LOGIC in your code, there is a chance that one of the 'echo' commands will be executed (in that 'if' block) before your call to 'header'.
    Rethink the flow of your program to avoid this.
    Going back to your original code:
    In the places where you wish to ECHO some output, instead, assign that to a string variable.
    Then, after the header, display the contents of that variable (with an 'echo' command).
    Don't be yourself. Be someone a little nicer. -Mignon McLaughlin, journalist and author (1913-1983)


    Git is for EVERYONE
    Literally, the best app for readers.
    Make Your P@ssw0rd Secure
    Leveraging SubDomains

  15. #15
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    then first i need to execute the header call and then i must give output.
    is that possible with the above code........

  16. #16
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    then first i need to execute the header call and then i must give output.
    is that possible with the above code........
    Yes it is. You can assign an empty string (called $message for example) at the start, so that you can perform your header work and add messages to the $message string. After you have finished doing your header work, it is then safe to output what you have stored in $message.
    Last edited by paul_wilkins; Sep 24, 2011 at 16:49.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  17. #17
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow reply

    i have modified the code.tell me whether the code i have modified is correct or not,,,,,
    PHP Code:
    <?php 
    session_start
    ();
    mysql_connect("localhost","root","") or die("mysql_error()");
    mysql_select_db("shopping") or die("mysql_error()"); 
    $result mysql_query("SELECT * from login where username='" $_POST['username'] . "' AND password='" $_POST['password'] . "'"); 
    $rows=mysql_num_rows($result);
    if(isset(
    $_SESSION['username']))
    {
     
    $messages"UserId : ".$_SESSION['username'];
     unset(
    $_SESSION['username']);

    else 
    {
      
    $_SESSION['username'] = time();
    }
    if (
    $rows 0

    session_register('username');
    $_SESSION['username'] = $_POST['username'];
    echo 
    $messages;
    header("Location:products.php");
    exit; 

    else

    //unsuccessful login 
    header("Location:login3.php");
    exit; 

    ?>

  18. #18
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ravi951 View Post
    i have modified the code.tell me whether the code i have modified is correct or not
    You need to delay all output (that's the echo commands) until after all of your header commands.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  19. #19
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    tell me in the above code how to check whether session exists or not. if not then using the time()
    i need to assign.is that possible in above code.

  20. #20
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,821
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    You are checking your sessions, OK...

    But remember also, that' some servers output blank spaces... if there are any blank spaces before your <?php tags.

    ALSO :
    PHP Code:
    <?php 
    session_start
    ();
    mysql_connect("localhost","root","") or die("mysql_error()");
    mysql_select_db("shopping") or die("mysql_error()"); 
    $result mysql_query("SELECT * from login where username='" $_POST['username'] . "' AND password='" $_POST['password'] . "'"); 
    $rows=mysql_num_rows($result);
    if(isset(
    $_SESSION['username']))
    {
     
    $messages"UserId : ".$_SESSION['username'];
     unset(
    $_SESSION['username']);

    else 
    {
      
    $_SESSION['username'] = time();
    }
    if (
    $rows 0

    session_register('username');
    $_SESSION['username'] = $_POST['username'];
    echo 
    $messages;
    header("Location:products.php");// THIS WILL NEVER WORK!!! you are outputing before the header(). 
    exit; 

    else

    //unsuccessful login 
    header("Location:login3.php");
    exit; 

    ?>
    You what.. I would actually rethink the entire logic of this check.

    PHP Code:
    if(isset($_SESSION['username']))
    {
     
    $messages"UserId : ".$_SESSION['username'];
     unset(
    $_SESSION['username']);

    if you UNSET $_SESSION['username'].. you are essentially unsettling the session for someone who is actually logged in. you probably realized this and followed it up with the $_SESSION['username']=time(); but then that means there will be session even if no one is logged in ( I just cant wrap my head as to why you would one to do this...

    Additionally ... you leave yourself wide open for $message being "user:".timestamp ( again i dont see why you would want this). Maybe there is a greater purpose for all of this, but I thought that I would point it out.
    Last edited by SpacePhoenix; Sep 22, 2011 at 17:50. Reason: swapped code tags for php tags

  21. #21
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reply

    then is it not possible to redirect my page to "products.php" page
    in the above code......

  22. #22
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,821
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    yes, because you have built in an error by echoing the message. Even the thinking is wrong ...

    Lets ASSUME PHP could work that way. The usser would never see your message anyway... as the page would be redirected immediately to products.php. So again this wouldn't be something you want to do, even if PHP allowed it.

  23. #23
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i am not bothered about the $message variable but first it should redirect my page to
    "products.php".

  24. #24
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,821
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    Just remove echo $message; and it will redirect fine then. The thing to keep in mind is that the variable will be lost in the redirect. So you wont be able to echo it in the next page either.

    Also note what I said about your logic.
    Additionally ... you leave yourself wide open for $message being "user:".timestamp ( again i dont see why you would want this).

  25. #25
    SitePoint Addict
    Join Date
    Jul 2011
    Location
    Hyderabad,India.
    Posts
    327
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if i remove echo $message every time it is re directing to header("Location:login3.php");
    why it is used for


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
  •