Help pls.

Discussion in 'PHP' started by rekha, Nov 25, 2009.

  1. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    In my site I am having a demo login.I processed all the code in one page.If there is empty session then I use demo login id to login.But I want this to make work only if I click the demo login button.
    How can I do this.
     
  2. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    As you will use session variables to store the session details, you can also use a variable to enable or disable the "demo login". Set the default value of the variable to "no". If you press the demo login button, change the value of that variable to "yes". While logging out of the "demo login" session by pressing logout, change the value of that variable again to "no".
     
  3. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    Thanks for your reply.I think it is a complex task.Can't it be done using referral.
     
  4. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    "Referral" in the sense? Can you explain?
     
  5. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    If the link is in the index page and if i click the link it should go to demo login.If I directly type the url in the address bar it should not go.

    How to do this?
     
  6. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    I don't know whether there is any other way than what I have mentioned in the previous posts. But let me know if you find another way :)

    I was using only session variables for such purposes earlier. It was not much difficult for me to do. Just lesser than 5 lines of code.
     
  7. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Can You post a sample code for me.Let me try if that's possible.
     
  8. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    What is the scripting language that you are using?
     
  9. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I am using php for setting session variables and javascript for validation and other things.
     
  10. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Will try out my code and let you know once it works.
     
  11. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    ok.waiting for your code snippet.
     
  12. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    I am posting here a small setup. Create 4 files as follows and load "index.php".

    index.php:
    PHP:
    <?php
    session_start
    ();
    session_register('flag');
    $_SESSION['flag'] = 'no';
    ?>
    <html>
    <head>
    </head>
    <body>
    Sample PHP page
    <a href="login_demo.php">Demo</a>
    </body>
    </html>
    login_demo.php:
    PHP:
    <?php
    session_start
    ();

    if(isset(
    $_SESSION['flag']) && $_SESSION['flag'] != "yes")
    {
        
    $_SESSION['flag'] = "yes";
        echo 
    '<meta http-equiv="Refresh" content="0;url=sample.php" />';
    }
    else
    echo 
    '<meta http-equiv="Refresh" content="0;url=index.php" />';
    ?>
    logout_demo.php:
    PHP:
    <?php
    session_start
    ();

    if(isset(
    $_SESSION['flag']) && $_SESSION['flag'] == "yes")
    {
        
    $_SESSION['flag'] = "no";
        unset(
    $_SESSION['flag']);
        
    session_destroy();
        echo 
    '<meta http-equiv="Refresh" content="0;url=index.php" />';
    }
    else
    echo 
    '<meta http-equiv="Refresh" content="0;url=index.php" />';
    ?>
    sample.php:
    PHP:
    <?php
    session_start
    ();

    if(isset(
    $_SESSION['flag']) && $_SESSION['flag'] != "yes")
    {
        echo 
    '<meta http-equiv="refresh" content="0;url=http://localhost/index.php" />';
    }
    else
    {
    ?>

    <html>
    <head>
    <title>Sample PHP Page</title>
    Session Variable Demo...
    </head>
    <body>
    <a href="logout_demo.php">Demo Logout</a>
    </body>
    </html>

    <?php
    }
    ?>
    Let me know whether I got your question correctly.
     
    shabbir likes this.
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  14. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Thanks shabbir, the article is really good and the code very clear as well.

    But is there any difference in using HTML's "META" tag and PHP's header() fundtion?
     
  15. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    I solved it by using http referer.

    Thanks.
     
  16. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Can you tell me what you are mentioning as http referer?

    If you had used a different way to solve your problem, reveal it here, so that every others will also know it.
     
  17. rekha

    rekha New Member

    Joined:
    Jan 17, 2008
    Messages:
    83
    Likes Received:
    0
    Trophy Points:
    0
    $SERVER['HTTP_REFERER'] determine where the request is coming from.In the index page I have the demo link.So I checked using http_referer that if the page comes from index.php,then automatically demo login will be done.That's it.
     
  18. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Good. Thanks for the update :)
     
  19. urstop

    urstop New Member

    Joined:
    Oct 17, 2007
    Messages:
    84
    Likes Received:
    0
    Trophy Points:
    0
    Istead of just checking for Empty session, you should check for POST variables to make sure that the user clicked the button.
     
  20. venami

    venami New Member

    Joined:
    Dec 26, 2008
    Messages:
    195
    Likes Received:
    10
    Trophy Points:
    0
    Occupation:
    Software Professional
    Location:
    India, Tamil Nadu, Cuddalore
    Home Page:
    http://mvenkatapathy.wordpress.com
    Rather than using buttons, usage of links would look good for this question. In that case, we have to use GET variables instead of POST variables.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice