Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

opening URL from php file

Options
  • 19-01-2006 10:21pm
    #1
    Closed Accounts Posts: 32


    hi just after starting to use php, its ok i thought it would alot different to other languages i've use but its not that diff.

    anyway, i have a login php file done, just adding features to it.

    i check a username and password against a database works fine. i retrieve a privilege from database for the user. student = 0 lecturer = 1

    if have an if statement which says:

    if (privilege is 0)
    open this webpage
    else
    open this webpage

    i cant find the code to open a page. i've messed around with fopen, header redirect but it doesnt work.

    tried javascript but couldnt get it to work.

    anyone able to point me in the right direction?

    cheers


Comments

  • Closed Accounts Posts: 33 sean_or99


    http://ie2.php.net/header

    <?php
    header("Location: http://www.example.com/"); /* Redirect browser */

    /* Make sure that code below does not get executed when we redirect. */
    exit;
    ?>

    If this is not working for you, then check what error is occuring.


  • Closed Accounts Posts: 32 VanStrummer


    This is my php file:

    <?php

    $name=$_POST;
    $pass=$_POST;

    mysql_connect("localhost", "", "") or die(mysql_error());
    echo "Connected to MySQL<br />";

    mysql_select_db("login") or die(mysql_error());
    echo "Connected to Database<br />";

    //Retrieve query from table
    $result = mysql_query("SELECT password, priv FROM logindetails WHERE username = '$name'")
    or die(mysql_error());

    $pass1=mysql_result($result,$i,"password");
    $priv1=mysql_result($result,$i,"priv");

    if ( $pass == $pass1 )
    {


    if ($priv1 == 0 )
    {
    //echo "<br />Student Privilege";
    //$handle = fopen("http://localhost/website/", "r");
    header("Location: http://localhost/website/"); /* Redirect browser */


    }
    else
    {
    echo "<br />Lecturer Page";
    }
    }

    ?>

    made changes like suggested, this is output:

    "; mysql_select_db("login") or die(mysql_error()); echo "Connected to Database
    "; //Retrieve query from table $result = mysql_query("SELECT password, priv FROM logindetails WHERE username = '$name'") or die(mysql_error()); $pass1=mysql_result($result,$i,"password"); $priv1=mysql_result($result,$i,"priv"); if ( $pass == $pass1 ) { if ($priv1 == 0 ) { echo "
    Student Privilege"; //$handle = fopen("http://localhost/website/", "r"); header("Location: http://localhost/website/"); /* Redirect browser */ exit; } else { echo "
    Lecturer Page"; } } ?>


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    I copied and pasted your code, exactly, I create a table called "logindetails", with the fields "username", "password" and "priv". populated it , performed a search and I got back exactly what was expected - no errors


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    When you say "Doesn't work", in what way does it not work? Does the page continue to load, do you get errors, etc etc?


  • Closed Accounts Posts: 32 VanStrummer


    i dont get errors, when tryin to execute code.

    the php file loads but all it shows is the php code and nothing else.


  • Advertisement
  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    so what you are saying is that when you access the php file it just shows you the code of the php file?

    ok then do the following.

    1. make sure php has been installed on the server you are working

    2. if its your own machine with a server set up - that you are accessing the page via the url http://localhost (or something similiar)


  • Registered Users Posts: 19,396 ✭✭✭✭Karoma


    ^ and test with phpinfo()


  • Closed Accounts Posts: 32 VanStrummer


    im using XAMMP

    the server and php are working fine, when i just echo some text in the if statement it prints it out fine, but if i try to load a URL in the if statement. all it it does is load the php file that is connected to the the html page and it prints out all the php code and shows it in the browser.

    basically its echoing all my code in the php file when i try to load a URL


  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    whats the url you are loading?


  • Closed Accounts Posts: 32 VanStrummer




  • Advertisement
  • Closed Accounts Posts: 4,655 ✭✭✭Ph3n0m


    Is it just not working for the Lecturer's login or either login?

    also have you tried what sean_or99 suggested above?


  • Closed Accounts Posts: 32 VanStrummer


    yeah i tried what sean said:

    if ($priv1 == 0 )
    {
    header("Location: http://localhost/website/"); /* Redirect browser */
    }
    else
    {
    echo "<br />Lecturer Page";
    }


    i've only tried logging in with privilege 0, just to get it work. Havent coded a page to load if lecturer logs in


  • Closed Accounts Posts: 32 VanStrummer


    problem solved, i dont know how put problem solved


    thnx for your help


  • Closed Accounts Posts: 9 jcunningham


    You cannot have any output (print,echo or html) before you call

    header("Location: ....

    The headers are sent before output begins, therefore if you try to change the header after output begins, you can't because the header has already been sent.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Yep - like jcunningham says - any output you have before the header() command will invalidate its use. Comment out all your prints before it and it will work....


Advertisement