SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    If string not x then send back to index.php

    Hi all I have a really simple form for my wrieframing:

    Index.php
    <form action="mainpage.php">
    <input name="pass" type="text">
    <input type="submit">
    </form>


    then when people click on submit then they get sent to mainpage.php

    On this page I want to check that the right text has been input in the pass field and if it isn't then send the user back to index.php
    I was thinking maybe I could pass the value on the url and then check for it, and if it isn't correct then relocate back to the index.php page. Here is my attempt:

    <?php print $_GET['pass']; if ($_GET['pass']=='specialword') echo ''; else (sendbackto index.php page)?>

    What should I do? (this is a very simple page I don't need to use a db at the moment)

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $pass
    =trim($_GET['pass']);
    if(
    $pass!='sPeCiaL_WorD'){
      
    header('location:/index.php');
      exit();
    }
    else{
      echo 
    'You have found the Graal!';
    }

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    To redirect use http://www.php.net/manual/en/function.header.php
    PHP Code:
    <?php 
      
    if ($_GET['pass'] != 'specialword') {
        
    header('Location: http://www.yoursite.com/index.php');
        exit();
      }
    ?>

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for those answers that is great. On my action page I did the following:

    PHP Code:
    <?php   if ($_POST['pass'] != 'specialword') {     header('Location: 

    http://www.mydomain.com/mypage.php'
    );     exit();   } ?>

    <?php include("header.php"); ?>
    but I get an error-Warning: Cannot modify header information - headers already sent by (output started at /home/content/html/index.php:1) in /home/content/html/index.php on line 1

    This must mean that I have duplicated the headers, how do i get rid of this error? Thank you

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    It means the script has already produced output before reaching the header function.
    Please post the whole action page script (at least up to the header function).

  6. #6
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi thanks for ther reply the error I get is Warning: Cannot modify header information - headers already sent by (output started at /home/index66.php:3) in /home/index66.php on line 3.
    This is the code with the html stripped out:

    Also the header is appearing with the error which it shouldn`t do.

    PHP Code:
    <?php include("header.php"); ?><?php   if ($_POST['pass'] != 'colombia') {     header('Location: http://www.mydomain.com/index66.php');     exit();   } ?>
     <div id="banner"><!--Banner--></div> <div id=main> <div id=col66Left> <div id=contentBody>

            <h2>You are in  <?php print $_GET['country']; if ($_GET['country']=='') echo 'UK'?> </h2>          <select name="select2" class="backinput" onChange="window.location.href=this.options[this.selectedIndex].value">            <option value=0 selected>Select country</option>            </select>      
      </h2>      </div>      
    <?php include("footer.php"); ?>

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    When someone asks for the complete script, don't strip the html out. Html is output.
    Anyway, try putting this part on top of the script, before anything else (even before any html):
    <?php if ($_POST['pass'] != 'colombia') { header('Location: http://www.mydomain.com/index66.php'); exit(); } ?>

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply I tried

    PHP Code:
    <?php if ($_POST['pass'] != 'colombia') { header('Location: 
    http://www.mydomain.com/index66.php'
    ); exit(); } ?>
    <?php 
    include("header.php"); ?>
    on the action page and it just relocates to http://www.mydomain.com/index66.php if a password is entered or not!! Confused what is going on?!! Thank you

  9. #9
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)

    If you don't enter a password, it's not equal to 'colombia', so it redirects.

    If you don't want that, you'll have to add a check to see if a password has been entered:
    PHP Code:
    <?php 
       
    if (isset($_POST['pass']) && $_POST['pass'] != 'colombia') { 
          
    header('Location: http://www.mydomain.com/index66.php'); 
          exit(); 
       } 
    ?>
    <?php 
    include("header.php"); ?>

  10. #10
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guido I tried it it doesn`t work-it doesnt matter if there is a passwordor not it still goes to the form action page. Why o why?!!!!

  11. #11
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Hmmm, I just noticed, when did you switch from $_GET to $_POST?

  12. #12
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,494
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Bayliss Trevor View Post
    Thanks guido I tried it it doesn`t work-it doesnt matter if there is a passwordor not it still goes to the form action page. Why o why?!!!!
    It ALWAYS goes to the form action page when you submit the form. That's where the form action page is for! To receive and handle the form data.
    And that's where you must put the code that handles the redirect.

  13. #13
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks it was the post giving me problems thank you!!

  14. #14
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi I seem to be running into some troubles.
    The code works in the action page (www.mydomain.com/act.php) if the form page (www.mydomain.com/index.php) sends it pass=specialword but throws an error


    Warning: Cannot modify header information - headers already sent by (output started at /home/content/html/act.php:1) in /home/content/html/act.php on line 1

    if "pass" is empty. Also if I I type in directly the form url then I can see the page without any problems. Why is this happening? Thank you in advance.
    Form page
    PHP Code:
    <form action="act.php">
    <
    input name="pass" type="text">
    <
    input type="submit">
    </
    form
    Action page
    PHP Code:
    <?php    if (isset($_GET['pass']) && $_GET['pass'] != 'specialword') {       header('Location: http://www.mydomain.com/');       exit();   
      }
      
    ?>
     <?php include("header.php"); ?>

  15. #15
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    headers already sent by (output started at /home/content/html/act.php:1)
    As an header must be sent before any HTML, as soon as html is sent to the browser, you cannot send a header to the browser, thus the "header('location')" fails.

    This message tells you that the file that sent html is /home/content/html/act.php at the line n&#176; 1.
    Often, it's just a space before the <?php opening tag.

  16. #16
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Tripy that sorts out that header error getting rid of the space before the opening tag. However I can still type in the act.php url and see the page without being send back to the form page. Why is that? Thanks

  17. #17
    SitePoint Evangelist
    Join Date
    Aug 2007
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I honestly don't see why. It should work.
    Could you put the whole page, rather than this short snippet.
    Maybe the problem is elsewhere.

  18. #18
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    It's your if statement:

    if (isset($_GET['pass']) && $_GET['pass'] != 'specialword')

    if it's set AND it's not 'specialword', if you dont enter the pass argument it will bypass the if.

    The solution would be:

    PHP Code:
    if (isset($_GET['pass') && $_GET['pass'] == 'specialword') {
     include(
    "header.php");
    }
    else {
        
    header('Location: http://www.mydomain.com/');
        exit();   


  19. #19
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Tom B I tried that and I get
    Parse error: syntax error, unexpected ')', expecting ']' in /home/content/html/act.php on line 2

    PHP Code:
    <?php 
     
    if (isset($_GET['pass') && $_GET['pass'] == 'specialword') {include("header.php");}else {    header('Location: http://www.mydomain.com/');    exit();   }  
      
    ?>

  20. #20
    SitePoint Evangelist
    Join Date
    Mar 2008
    Posts
    551
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was a typo I worked it out thanks
    PHP Code:
    <?php 
     
    if (isset($_GET['pass']) && $_GET['pass'] == 'specialword') {include("header.php");}else {    header('Location: http://www.mydomain.com/');    exit();   }  
      
    ?>


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
  •