1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Wheres the problem in these codes?

Discussion in 'PHP' started by Gravereaper, Dec 3, 2006.

  1. #1
    Ok i've just started learning PHP and already making a site which is 40% completed, but i'am having problems making the admin section. Please check the codes below:
    if ($login=="true")
    
    echo "hi";
    
    else
      { 
    echo "Please enter your password to continue:</BR>";
    echo "<form action='admin.php' method='post'>
    <input type='text' name='pass' /></br><input type='submit' value='Login' />
    </form>";
    $temppass = $_POST["pass"];
    if ($temppass=="ee") 
    $login=="true";
    else
    echo "Incorrect Password";
     }
    ?>
    Code (markup):
    All i tried to do is that when admin.php is opened, this password editbox shows up and if someone enters the correct password the contents of the page are changed to something else ( i've used "HI" for the contents to test ) and if the password is incorrect it just echo that text. It dose'nt seems to work anyways, can anybody debug it?
     
    Gravereaper, Dec 3, 2006 IP
  2. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Keep in mind the php script is ran, then outputted to the user.. so in your code you're trying to update it "on the fly" (okay horrible explanation.. bear with me).

    Essentially you're saying if $login isn't true, display form.. then compare form with "ee".. if it's correct set $login to true.. problem is the script has already been ran so it's not going to check $login again until the next time the script is ran. (I beg someone whose better at explanations clarify this, but ya!)

    
    <?php
    
          if (@$_POST['pass'] == "ee") {
    
             echo "Welcome home hot cakes!";
    
          } else {
    
             echo "Die you scallywag! (form here)";
    
          }
    
    ?>
    
    PHP:
    Please keep in mind this isn't a great way to make a login system.. it's good for learning.. but theres plenty of examples out on Google on how to make decent ones. (Normally I would do this differently, but I was trying to take what you were attempting to do and make it work..)
     
    CodyRo, Dec 3, 2006 IP
  3. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Then where will i ask for the password?
     
    Gravereaper, Dec 3, 2006 IP
  4. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In the else statement.. where it says "Die you scallywag! (form here)"
     
    CodyRo, Dec 3, 2006 IP
  5. Luke

    Luke Peon

    Messages:
    111
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    <?
    if($_POST["pass"]){
    $temppass = $_POST["pass"];
    if ($temppass=="ee") 
    $login="true"; // You had two == which just checks a variable rather then one = which sets a variable.
    else
    echo "Incorrect Password";
    }
    
    if ($login=="true")
    
    echo "hi";
    
    else
      { 
    echo "Please enter your password to continue:</BR>";
    echo "<form action='admin.php' method='post'>
    <input type='text' name='pass' /></br><input type='submit' value='Login' />
    </form>";
     }
    ?>
    
    PHP:
    Quick fix to your existing code.
     
    Luke, Dec 4, 2006 IP
  6. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #6
    The above would only work if there were no forms on the page, heres a commented session authentication page....

    
    <? 
    session_start(); // Start a session
    
    $password = "password"; // Could also be set via sql
    
    if ($_GET['ax'] == "logout")
    { // Logout routine, typically stored in logout.php
    	session_destroy(); // Destroy session
    	header("location: admin.php"); // Redirect browser
    	exit; // Exit cleanly
    }
    
    if ( !$_POST && !$_SESSION['auth'] )
    { // This html is displayed if the page isn't posted, or
      // if the user failed authentication
    	echo "<form action='' method='post'>\n
    		  <input type='text' name='pass' />\n
    		  </br><input type='submit' value='Login' />\n
    		  </form>\n";	
    }
    elseif( $_POST['pass'] == $password )
    { // The password matched the one stroed
    	$_SESSION['auth'] = true; // Set session authenticated
    	header("location: admin.php"); // Redirect to content
    	exit; // Cleanly exit
    }
    elseif($_SESSION['auth'])
    { // User is already authorized, this is where the main content would go
    	echo "Hello, you're logged in\n<br>";
    	echo "<a href=\"admin.php?ax=logout\">Logout</a>";
    }
    else { // Redirect browser password was wrong
    		header("location: admin.php");
    		exit;
    	 }
    ?>
    
    PHP:
    You can then carry your authentication to other content so long as session_start() is at the top of the page.....
     
    krakjoe, Dec 4, 2006 IP
  7. Luke

    Luke Peon

    Messages:
    111
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Be gentle Krakjoe, you'll overload the poor boy.
     
    Luke, Dec 4, 2006 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    give a man a fish and all that..... :)
     
    krakjoe, Dec 4, 2006 IP
  9. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    CodyRo codes worked just fine, thanks everybody :)

    EDIT:
    I have another problem now:
    <?php
    if (@$_POST['pass'] == "apass") {
    
      echo "Welcome!</BR>";
      echo "<form action='admin.php' method='post'>";
      echo "Submit to category 1:</BR><input type='text' name='et1' style='position:absolute;width:394px;z-index:1' /></BR>";
      echo "Submit to category 2:</BR><input type='text' name='et2' style='position:absolute;width:394px;z-index:1' /></BR>";     
      echo "Submit to category 3:</BR><input type='text' name='et3' style='position:absolute;width:394px;z-index:1' /></BR>"; 
      echo "Submit to category 4:</BR><input type='text' name='et4' style='position:absolute;width:394px;z-index:1' /></BR>"; 
      echo "Submit to category 5:</BR><input type='text' name='et5' style='position:absolute;width:394px;z-index:1' /></BR>"; 
      echo "Submit to category 6:</BR><input type='text' name='et6' style='position:absolute;width:394px;z-index:1' /></BR></BR>"; 
      echo "<input type='submit' value='Submit All ets'>";
    } else {
    echo "Please enter your password to continue:</BR>";
    echo "<form action='admin.php' method='post'>
    <input type='text' name='pass' /></br><input type='submit' value='Login' />
    </form>";
    
    }
    
    $et1 = $_POST["et1"];
    $et2 = $_POST["et2"];
    $et3 = $_POST["et3"];
    $et4 = $_POST["et4"];
    $et5 = $_POST["et5"];
    $et6 = $_POST["et6"];
    
    if ( $et1 = " " ) 
    echo "Category 1 is blank";
    elseif ( $et2 = " " ) 
    echo "Category 2 is blank";
    else 
    echo "nothing looks blank";
    ?>
    Code (markup):
    These codes will simply perform an instruction for every category editbox which is'nt left blank, i'll add that instruction later but right now these codes dose'nt seem to work :(
     
    Gravereaper, Dec 4, 2006 IP
  10. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #10
    if ( $et1 = " " )

    == is a comparison operator, not =
     
    krakjoe, Dec 4, 2006 IP
  11. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #11
    use: <br />
    instead of </BR>
     
    bobby9101, Dec 4, 2006 IP
  12. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #12
    And what can i use to detect a blank edit box? " " dose'nt seems to work
     
    Gravereaper, Dec 5, 2006 IP
  13. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #13
    if ($var == "")
    {
    // if input is empty this will return true;
    }
     
    krakjoe, Dec 5, 2006 IP
  14. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ok i wrote some more codes for the program and again its not working as it should.. In the codes below the program is simply suppose to 'APPEND' text to certain .txt files when its corresponding edit box is filled with some text. But this thing is not working like that, maybe its because of the password prompt?
    <?php
    function submitet($et,$data) 
    {
      $filename = "$et";
      $fo = fopen($filename,'a');
      $dataString = $data;
      fwrite($fo,$dataString);
    }
    
    echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<A HREF='index.php'><IMG SRC='daetssite.gif' border='0'></A><BODY BACKGROUND='bground.jpg' bgproperties='fixed'><STYLE>
    BODY { background-repeat: no-repeat }
    </STYLE></BR>";
    
    if (@$_POST['pass'] == "apass") {
    
      echo "<div style='position:absolute; overflow:hidden; left:0px; top:141px; width:136px; height:298px; z-index:3'>
      <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
      <DIV>Submit to Category 1:</DIV>
      <DIV>&nbsp;</DIV>
      <DIV>Submit to Category 2:</DIV>
      <DIV>&nbsp;</DIV>
      <DIV>Submit to Category 3:</DIV>
      <DIV>&nbsp;</DIV>
      <DIV>Submit to Category 4:</DIV>
      <DIV>&nbsp;</DIV>
      <DIV>Submit to Category 5:</DIV>
      <DIV>&nbsp;</DIV>
      <DIV>Submit to Category 6:</DIV>
      </FONT>
      </DIV>
      <div style='position:absolute; overflow:hidden; left:490px; top:142px; width:288px; height:205px; z-index:8'>
      <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
      <DIV>Submit to Top 10:</DIV>
      </FONT>
      </DIV>
      <div style='position:absolute; overflow:hidden; left:491px; top:209px; width:283px; height:22px; z-index:10'>
      <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
      <DIV>et of the Day:</DIV>
      </FONT>
      </DIV>
      <div style='position:absolute; overflow:hidden; left:381px; top:115px; width:96px; height:29px; z-index:13'>
      <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
      <DIV><FONT color=#0000FF><A href='messages.php'>Read Messages</A></FONT></DIV>
      </FONT>
      </DIV>
      <form name='form1'>
      <input name='et1' type='text' style='position:absolute;width:317px;left:136px;top:143px;z-index:1'>
      <input name='et2' type='text' style='position:absolute;width:317px;left:136px;top:171px;z-index:2'>
      <input name='et3' type='text' style='position:absolute;width:317px;left:136px;top:205px;z-index:4'>
      <input name='et4' type='text' style='position:absolute;width:317px;left:136px;top:233px;z-index:5'>
      <input name='et5' type='text' style='position:absolute;width:317px;left:136px;top:266px;z-index:6'>
      <input name='et6' type='text' style='position:absolute;width:317px;left:136px;top:294px;z-index:7'>
      <input name='topten' type='text' style='position:absolute;width:284px;left:491px;top:165px;z-index:9'>
      <input name='dayet' type='text' style='position:absolute;width:284px;left:492px;top:232px;z-index:11'>
      <input type='submit' value='Submit All' style='position:absolute;left:588px;top:296px;z-index:12'>
      </form>";
    
    }
    
    else
    { 
      echo "Please enter your password to continue:</BR>";
      echo "<form action='admin.php' method='post'>
      <input type='text' name='pass' /></br><input type='submit' value='Login' />
      </form>";
      $temppass = $_POST["pass"];
      if ($temppass=="ee") 
      $login=="true";
      else
      echo "Incorrect Password";
    }
    
    $et1 = $_POST["et1"];
    $et2 = $_POST["et2"];
    $et3 = $_POST["et3"];
    $et4 = $_POST["et4"];
    $et5 = $_POST["et5"];
    $et6 = $_POST["et6"];
    $topten = $_POST["topten"];
    $dayet = $_POST["dayet"];
    
    if ( $et1 != "" ) 
    submitet("et1.txt",$et1);
    elseif ( $et2 != "" ) 
    submitet("et2.txt",$et2);
    elseif ( $et3 != "" )
    submitet("et3.txt",$et3);
    elseif ( $et4 != "" )
    submitet("et4.txt",$et4);
    elseif ( $et5 != "" )
    submitet("et5.txt",$et5);
    elseif ( $et6 != "" )
    submitet("et6.txt",$et6);
    elseif ( $topten != "" )
    submitet("topten.txt",$topten);
    elseif ( $dayet != "" )
    submitet("dayet.txt",$dayet);
    ?>
    
    Code (markup):
     
    Gravereaper, Dec 7, 2006 IP
  15. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #15
    I'am still stuck, can anybody debug it?
     
    Gravereaper, Dec 9, 2006 IP
  16. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #16
    I'd do it differently, I can't make sense of the bit at the bottom.

    Make a template of what you want to write on to the file everytime, for instance,
    
    $template =<<<TEMPLATE
    
    Name : {NAME}
    Age : {AGE}
    Next : {NEXT}
    
    TEMPLATE;
    
    PHP:
    and then write a function to parse the template like :
    
    function parseTemplate ($template)
    {
    $newTemplate = preg_replace( "/{NAME}/", stripslashes(trim($_POST['name'])), $template );
    $newTemplate = preg_replace( "/{AGE}/", stripslashes(trim($_POST['age'])), $newTemplate );
    return $newTemplate ;
    }
    
    PHP:
    and when you write the text to a file, remember to include literal \n at the end of the template to keep things tidy, what's this for anyway, there might be a better way ?
     
    krakjoe, Dec 9, 2006 IP
  17. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Your script looks something other then what i was trying to do, heres in detail what my code should actually do:

    1)Asks for a login, and if the login is correct displays the form ( with multiple edit boxes to submit text to particular text files, a different .txt file for every edit box )

    2)Upon hitting the Submit button, ( for e.g. ) if the first edit box has something written then a text should be written in First.txt, if nothing is written in the second edit box then the script simply skips to the third one and if something is written in the third editbox the script writes the text in the third editbox to Third.txt ( file names are different in the actual script )

    But when i hit the Submit button everything works well, besides it dose'nt actually writes anything in the text files..

    Did this helped?
     
    Gravereaper, Dec 9, 2006 IP
  18. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #18
    
    <? session_start(); // Start a session
    
    $password = "password"; // Could also be set via sql
    
    function submitet($et,$data) 
    {
      $fo = fopen($et,'a');
      if(!$fo)
      {
    	die("Cannot open $et");	
      }
      if(!fwrite($fo,$data))
      {
    	die("Cannot write to $et");	
      }
      fclose($fo);
    }
    function doWrite()
    {
    	if ( $_POST['et1'] != "" ) submitet("et1.txt", $_POST['et1']);
    	elseif ( $_POST['et2'] != "" ) submitet("et2.txt", $_POST['et2']);
    	elseif ( $_POST['et3'] != "" ) submitet("et3.txt", $_POST['et3']);
    	elseif ( $_POST['et4']!= "" ) submitet("et4.txt",$_POST['et4']);
    	elseif ( $_POST['et5']!= "" ) submitet("et5.txt",$_POST['et5']);
    	elseif ( $_POST['et6'] != "" ) submitet("et6.txt",$_POST['et6']);
    	elseif ( $_POST['topten'] != "" ) submitet("topten.txt", $_POST['topten'] );
    	elseif ( $_POST['dayet'] != "" ) submitet("dayet.txt",$_POST['dayet']);
    	header("location:" . $_SERVER['PHP_SELF'] );
    	exit;
    }
    
    if ($_GET['ax'] == "logout")
    { // Logout routine, typically stored in logout.php
        session_destroy(); // Destroy session
        header("location : " . $_SERVER['PHP_SELF '] ); // Redirect browser
        exit; // Exit cleanly
    }
    
    if ( !$_POST && !$_SESSION['auth'] )
    { // This html is displayed if the page isn't posted, or
      // if the user failed authentication
        echo "<form action='' method='post'>\n
              <input type='text' name='pass' />\n
              </br><input type='submit' value='Login' />\n
              </form>\n";   
    }
    elseif( $_POST['pass'] == $password )
    { // The password matched the one stroed
        $_SESSION['auth'] = true; // Set session authenticated
        header("location: calc.php"); // Redirect to content
        exit; // Cleanly exit
    }
    elseif($_SESSION['auth'])
    { // User is already authorized, this is where the main content would go
    	if($_POST['dosuff'])
    	{
     		doWrite();
    	}
    	else {
    			echo "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<A HREF='index.php'><IMG SRC='daetssite.gif' border='0'></A><BODY BACKGROUND='bground.jpg' bgproperties='fixed'><STYLE>
    	BODY { background-repeat: no-repeat }
    	</STYLE></BR>";
    			echo "<div style='position:absolute; overflow:hidden; left:0px; top:141px; width:136px; height:298px; z-index:3'>
    				  <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
    				  <DIV>Submit to Category 1:</DIV>
    				  <DIV>&nbsp;</DIV>
    				  <DIV>Submit to Category 2:</DIV>
    				  <DIV>&nbsp;</DIV>
    				  <DIV>Submit to Category 3:</DIV>
    				  <DIV>&nbsp;</DIV>
    				  <DIV>Submit to Category 4:</DIV>
    				  <DIV>&nbsp;</DIV>
    				  <DIV>Submit to Category 5:</DIV>
    				  <DIV>&nbsp;</DIV>
    				  <DIV>Submit to Category 6:</DIV>
    				  </FONT>
    				  </DIV>
    				  <div style='position:absolute; overflow:hidden; left:490px; top:142px; width:288px; height:205px; z-index:8'>
    				  <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
    				  <DIV>Submit to Top 10:</DIV>
    				  </FONT>
    				  </DIV>
    				  <div style='position:absolute; overflow:hidden; left:491px; top:209px; width:283px; height:22px; z-index:10'>
    				  <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
    				  <DIV>et of the Day:</DIV>
    				  </FONT>
    				  </DIV>
    				  <div style='position:absolute; overflow:hidden; left:381px; top:115px; width:96px; height:29px; z-index:13'>
    				  <FONT style='font-size:10pt' color=#000000 face='Tahoma'>
    				  <DIV><FONT color=#0000FF><A href='messages.php'>Read Messages</A></FONT></DIV>
    				  </FONT>
    				  </DIV>
    				  <form name='form1' method=\"post\" action=\"\">
    				  <input name='et1' type='text' style='position:absolute;width:317px;left:136px;top:143px;z-index:1' />
    				  <input name='et2' type='text' style='position:absolute;width:317px;left:136px;top:171px;z-index:2' />
    				  <input name='et3' type='text' style='position:absolute;width:317px;left:136px;top:205px;z-index:4' />
    				  <input name='et4' type='text' style='position:absolute;width:317px;left:136px;top:233px;z-index:5' />
    				  <input name='et5' type='text' style='position:absolute;width:317px;left:136px;top:266px;z-index:6' />
    				  <input name='et6' type='text' style='position:absolute;width:317px;left:136px;top:294px;z-index:7' />
    				  <input name='topten' type='text' style='position:absolute;width:284px;left:491px;top:165px;z-index:9' />
    				  <input name='dayet' type='text' style='position:absolute;width:284px;left:492px;top:232px;z-index:11' />
    				  <input type='submit' value='Submit All' style='position:absolute;left:588px;top:296px;z-index:12' />
    				  <input type=\"hidden\" name=\"dosuff\" value=\"1\" /> 
    				  </form>";	
    		 }	
    }
    else { // Redirect browser password was wrong
            header("location : " . $_SERVER['PHP_SELF '] );
            exit;
         }
    
    ?>
    
    PHP:
    You're code wouldn't work for me, firstly because you forgot action and method in the form tag, and secondly becasue I got landed at the authtication form, so I took what I posted the other day and made it work for you.
     
    krakjoe, Dec 9, 2006 IP
  19. Gravereaper

    Gravereaper Banned

    Messages:
    450
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #19
    I tried running your codes but it displayed a CALC.PHP not found error
     
    Gravereaper, Dec 9, 2006 IP
  20. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #20
    search and replace calc.php with the name of the script
     
    krakjoe, Dec 10, 2006 IP