SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast lrempel's Avatar
    Join Date
    Jan 2005
    Location
    Vancouver
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php equivalent of <cflocation> ?

    Hello,

    I am just learning php (I know coldfusion), and am trying to figure something out. I want to run a script containing an if block, part of which if it runs I want the user to be sent to another php page (the coldfusion equivalent is <cflocation url="http://myurl.com"> for those who may also know coldfusion). I also don't want the rest of the script on the first page to run.

    Here is some code that might make it a little more clear:

    PHP Code:
    if($_POST['submit']){
            
    //process form
            
    $loginsql "SELECT * FROM tbl_admin";
            
    $loginresult mysql_query($loginsql);
            
    $loginrow mysql_fetch_array($loginresult);
            
            if(
    $loginrow['fld_username'] === $_POST['username'] && $loginrow['fld_password'] === $_POST['passwrd']) {
            
    //HERE IS WHERE I WANT THE USER TO GO TO A NEW PAGE
            
    } else {
                
    $message "Your username and/or password are incorrect. Please try again.";
            }
        } 
    Thanks.

  2. #2

    Join Date
    Oct 2003
    Location
    €uroLand
    Posts
    1,340
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    header('Location: http://www.something.smt'); 

  3. #3
    SitePoint Enthusiast lrempel's Avatar
    Join Date
    Jan 2005
    Location
    Vancouver
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    drzoid,

    I tried that, but it doesn't seem to be working. I have the if block as part of a self-submitting form (ie: check to see if form has been submitted, if yes, and authentication passes send to another page, if authentication failed write message to variable - then display login form again). Is there a problem using header('Location: http://www.something.com') in a self-submitting form?

    I have tested to make sure my if statement is evaluating to true, but it still doesn't seem to redirect the user.

    Here is the code again:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <?php 
        
        
    if($_POST['submit']){
            
    //process form
            
    $loginsql "SELECT * FROM tbl_admin";
            
    $loginresult mysql_query($loginsql);
            
    $loginrow mysql_fetch_array($loginresult);
            echo 
    $loginrow['fld_username'];
            echo 
    $loginrow['fld_password'];
            echo 
    $_POST['username'];
            echo 
    $_POST['passwrd'];
            
            if(
    $loginrow['fld_username'] === $_POST['username'] && $loginrow['fld_password'] === $_POST['passwrd']) {
                echo 
    "Inside the IF block - should redirect";
                
    header('Location: http://www.mysite.com/test/adminNewMain.php');
            } else {
                echo 
    "No match";
                
    $message "Your username and/or password are incorrect. Please try again.";
            }
        }
    ?>
    <html>
    <head>
    <title>Admin Login</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <form action="<?php echo $PHP_SELF?>" method="post">
    <table align="center" border="0">
        <tr>
            <td align="center">Welcome to the Website Administrator. Please log-in to continue.</td>
        </tr>
        <tr>
            <td align="center"><?php echo $message ?></td>
        </tr>
        <tr>
            <td align="center">
                <table>
                    <tr>
                        <td align="right">Username:</td>
                        <td><input type="text" name="username"></td>
                    </tr>
                    <tr>
                        <td align="right">Password:</td>
                        <td><input type="password" name="passwrd"></td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td align="center"><input type="submit" name="submit" value="Login"></td>
        </tr>
    </table>
    </form>        
    </body>
    </html>
    (The url in the header tag is not real, and I took out the mysql connection info, but I know that works.)

    If anyone can see problems please let me know.

    Thanks.

  4. #4
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Madison, WI
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    header() will only work if you haven't output anything yet in the script, so your doctype declaration and the numerous echos before the header() call will prevent it from working

  5. #5
    SitePoint Enthusiast lrempel's Avatar
    Join Date
    Jan 2005
    Location
    Vancouver
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks jinkas - that was the problem.

    so, just for future reference: there is no way in php to send the user to another location (after running some scripts - not user controlled) if there has been output generated?

  6. #6
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not that I know of. You could always pull a Javascript:
    window.location.href='http://website.com' but that's obviously not as good a solution.

  7. #7
    SitePoint Zealot
    Join Date
    Aug 2004
    Location
    Madison, WI
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you store the output in a buffer you can redirect and have output. however, a user can't see output unless it is somehow sent to the browser, and as soon as output hits the browser then header() (and other pre-output functions, such as setcookie()) will no longer work

    basically, you can't have user-viewable output and a header() call in the same page

    you could maybe try buffering your output (either in a variable or using ob_start()), then flush the buffer to a file if header() needs to be called. on the new page you could load the contents of the file, but still the best you could achieve is "[content] you have been redirected" rather than "[content] you will be redirected"


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
  •