Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: regarding php session

  1. #11
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    hi please tell me how to modify the code as u mentioned

  2. #12
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    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='" mysql_escape_string($_POST['username']) . "' AND password='" mysql_escape_string($_POST['password']) . "'"); 

    // The array of data returned from MySQL.
    $r mysql_fetch_array($result);

    $rows=mysql_num_rows($result);
    if(!isset(
    $_SESSION['username']))
    {

    // Check if the username/password matches, THEN set a session.
    if($rows 0) {
    // Set session.
    $_SESSION['username'] = $_POST['username'];
    }

    header("Location:products.php");

    else 
    {
    $_SESSION['username']= time();
    header("Location:products.php");
    }
    ?>
    - Josh

  3. #13
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reeply

    thank you it helped me a lot

  4. #14
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    in line $r = mysql_fetch_array($result);
    the variable $r is never used any where in the code u posted

  5. #15
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Whoops. I meant to change this line:
    PHP Code:
    $_SESSION['username'] = $_POST['username']; 
    ...to this:
    PHP Code:
    $_SESSION['username'] = $r["username"]; 
    - Josh

  6. #16
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    whats the difference $_POST['username'] and $r['username']
    make

  7. #17
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    They're different variables. $r is the row from the mysql query. $_POST is the post data as originally sent during the request from the browser (usually from a form).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #18
    Join Date
    Jul 2011
    Location
    hyderabad,India
    Posts
    58
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reply

    Quote Originally Posted by JShor View Post
    Whoops. I meant to change this line:
    PHP Code:
    $_SESSION['username'] = $_POST['username']; 
    ...to this:
    PHP Code:
    $_SESSION['username'] = $r["username"]; 
    hi,
    in the else part below
    else
    {
    $_SESSION['username']= time();
    header("Locationroducts.php");
    }
    this will be reached when both the username and password not matched.right..
    my question is if both username and password are internally in the database
    then there is no way of changing it.
    since there is no way the passwords matching fails
    then it will never direct to else part.is that so...

  9. #19
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The query searches for the password based on the submitted (POST) username and submitted (POST) password. If any result is found that means the correct username and password were submitted. So, checking for a result is the same as verifying that the stored password is the same as the submitted password.

    Honestly, I think you need to go through some introductory PHP tutorials, maybe some for MySQL also, so that you understand the basics. It's not easy to start with complicated scripts, but it won't at all to keep guessing about these things.


    A good exercise to learn is to go through code and add comments for every line. What does that line do? Why is it there? How does it interact with code before and after? Once you understand what each line does, then you need to think about how it all fits together toward your goal. Then you'll begin to understand how PHP works.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •