need help with syntax

Discussion in 'PHP' started by PHP Newbie, May 17, 2010.

  1. #1
    Need help with syntax - very new to php

    I am trying to use a field (redirect) in mysql to redirect a user to a specific page upon login. Each user has their own private page.

    My code isn't taking me to my page and I've been unable to determine the correct syntax. Any help greatly appreciated.

    <?php
    session_start();

    include("include/s_session.php");

    $sql = "SELECT redirect FROM users WHERE username = '$_SESSION[username]' AND password = 'md5($_SESSION[password])'";
    $result = mysql_query($sql) or die(mysql_error());

    if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME)
    {
    header('Location: $result");
    }
    else
    {
    header ("location: s_loginForm.php");
    }
    ?>

    Thanks in advance.
     
    PHP Newbie, May 17, 2010 IP
  2. vetrivel

    vetrivel Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Replace this line:
    header('Location: $result");
    with this line:
    header("Location: $result");



     
    vetrivel, May 17, 2010 IP
  3. PHP Newbie

    PHP Newbie Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the input. I have changed that line and now get a msg the the requested file is not found on the server ???
     
    PHP Newbie, May 17, 2010 IP
  4. PHP Newbie

    PHP Newbie Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The redirect data in mysql is /folder/sub-folder/file.php

    could this be the problem?
     
    PHP Newbie, May 17, 2010 IP
  5. vetrivel

    vetrivel Peon

    Messages:
    147
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Add
    print_r($result);
    exit;
    before :
    header("Location: $result");




    Then check the output in your screen which show the URL to which you need to redirect and also check that whether that URL is valid.
     
    vetrivel, May 17, 2010 IP
  6. PHP Newbie

    PHP Newbie Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    OK ... Now i'm really confused. value is populated with "Resource ID #14"

    No idea where that comes from??
     
    PHP Newbie, May 17, 2010 IP
  7. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #7
    Try this

    
    ?php
    session_start();
    
    include("include/s_session.php");
    
    $sql = "SELECT redirect FROM users WHERE username = '$_SESSION[username]' AND password = 'md5($_SESSION[password])'";
    $result = mysql_query($sql) or die(mysql_error());
    
    if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME)
    {
    header("Location: $result");
    }
    else
    {
    header ("location: s_loginForm.php");
    }
    ?>
    
    
    PHP:
     
    roopajyothi, May 17, 2010 IP
  8. PHP Newbie

    PHP Newbie Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks.

    I've made some progress and I now see that the query is returning the value from the table plus the resource info.

    The result looks like this: /folder/sub-folder/file.php Resource id #14
    How do I get rid of the resource portion???? and just have the data from the redirect field??

    Here is the code as it is now:

    <?php
    session_start();

    include("include/s_session.php");

    $result = mysql_query("SELECT redirect FROM users WHERE password = '$_SESSION[password]'") or die(mysql_error());
    $row = mysql_fetch_array($result);
    echo $row['redirect'];

    if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME)
    {
    header("Location: $result");
    }
    else
    {
    header ("location: s_loginForm.php");
    }

    mysql_free_result($result);
    ?>
     
    PHP Newbie, May 17, 2010 IP
  9. PHP Newbie

    PHP Newbie Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Here is the final solution. This code works!!

    <?php
    //Start session
    session_start();

    include("include/s_session.php");

    if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME)
    {
    // user is logged in
    }
    else
    {
    header ("location: s_loginForm.php");
    }

    $result = mysql_query("SELECT redirect FROM users WHERE password = '$_SESSION[password]'") or die(mysql_error());

    $row = mysql_fetch_assoc($result);
    header("Location:" .$row['redirect']);

    mysql_free_result($result);
    ?>
     
    PHP Newbie, May 18, 2010 IP
  10. roopajyothi

    roopajyothi Active Member

    Messages:
    1,302
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    80
    #10
    Nope it should be like this

    
    
    <?php
    session_start();
    
    include("include/s_session.php");
    
    if(isset($_SESSION['username']) && isset($_SESSION['password']) && isset($_SESSION['userid']) && $_SESSION['username'] != GUEST_NAME)
    {
    }
    else
    {
    header ("location: s_loginForm.php");
    }
    
    $result = mysql_query("SELECT redirect FROM users WHERE password = '$_SESSION[password]'") or die(mysql_error());
    
    $row = mysql_fetch_assoc($result);
    // Major Fix to redirect
    header("Location:" .$row['redirect']);
    
    mysql_free_result($result);
    ?> 
    
    
    PHP:
     
    roopajyothi, May 18, 2010 IP
  11. PHP Newbie

    PHP Newbie Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thank you for your input. Much appreciated.

    See my post from 2:06pm today. We came up with the same solution :)
     
    PHP Newbie, May 18, 2010 IP