Page 1 of 2 12 LastLast
Results 1 to 10 of 19

Thread: regarding php session

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

    Default regarding php session

    tell me how to do
    1)check session is present or not
    2)until there is a session then it must redirect to page "products.php"
    3)if there is no session also create one session and then redirect to the
    page "products.php"

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

    Default

    PHP Code:
    <?php

    session_start
    ();

    if(isset(
    $_SESSION['session_variable_name'])) {
    // session is set.
    } else {
    // Session is not set.

    // Create session.
    $_SESSION['session_name'] = "content";

    //  Redirect to "products.php".
    header("Location: products.php");
    }

    ?>
    - Josh

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

    Default reply

    when session_start() itself creates session then why again u explicitly
    created session as below
    // Create session.
    $_SESSION['session_name'] = "content";

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

    Default

    There's no need for anything that complex, especially if you don't understand sessions yet. There are very few circumstances in which you need to check if a session exists, or anything like that.

    This is very simple. Add this to the very beginning of every page on your site. Remember that you do not need it on included pages because the session will already be started by the main page.

    <?php session_start(); ?>

    Or, if you have more PHP:

    <?php
    session_start();
    .....
    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

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

    Default reply

    when we write a code as
    <?php
    session_start();
    //program continue
    //
    ?>
    then we will be in the session only till we close the window or when we
    explicitly declare session_destroy()
    right

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

    Default

    Generally, yes. The session can also end if it is not renewed within a certain amount of time, sometimes 15 minutes, sometimes longer. You can control some of this if you want, using php.ini (or some functions in your pages), but usually it will not matter. Closing the window also won't always destroy the session, but closing the browser (entire program) will.


    Sessions are easy to use, and they last for a "session". You can also use session_destory() to end it intentionally. Everything else is not usually important.
    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

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

    Default reply

    then what is the difference between session_destroy() and session_unset()

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

    Default

    session_destroy() turns sessions off. session_unset() deletes the information in the session but continues the session. It is like session_destory() + session_start().

    For information on specific functions, please learn to use php.net. The function reference section is easy to use and has all of the information you will need about most functions.
    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

  9. The Following User Says Thank You to djr33 For This Useful Post:

    ravi951 (09-24-2011)

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

    Default

    Quote Originally Posted by JShor View Post
    PHP Code:
    <?php

    session_start
    ();

    if(isset(
    $_SESSION['session_variable_name'])) {
    // session is set.
    } else {
    // Session is not set.

    // Create session.
    $_SESSION['session_name'] = "content";

    //  Redirect to "products.php".
    header("Location: products.php");
    }

    ?>
    hi
    tell me whether this code 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']))
    {
    $_SESSION['username'] = $_POST['username'];
    header("Location:products.php");

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

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

    Default

    Your code is valid, but it seems like setting the username as a POST variable is a huge security issue. You should check whether the username:
    1. Exists.
    2. Matches a POSTed password.

    You should also sanitize the input. A malicious user can create HTTP/MySQL injections.

    Also, this doesn't work: or die("mysql_error()"); . mysql_error() is a function, not a variable. Remove the double quotes and it should be fine.
    - Josh

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
  •