Thread: Php

  1. #31
    Registered User
    Join Date
    Jul 2004
    Posts
    169
    yes thats what i have been doing, i have a book on php, though it uses php 3 or 4 or something so its sligthyl old. but alot of it was useful and it went through the importance of checking ever command like that

  2. #32
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Also, php.net is very useful.

    Just go to php.net/function, to get the man page on it along with user comments, etc.

  3. #33
    Registered User
    Join Date
    Jul 2004
    Posts
    169

    strange if statemen behavoir

    i have this part in my code. which DID work then out of nowhere it stopped working and i cant understand it. this is apart of my registration process to make sure the user input the right kind of information

    PHP Code:
    if(($username_check 0) || ($usersite_check 0) || ($usersite_check2==0) || ($password_check==0) || ($email_check2==0))
              {

                   if(
    $username_check 0){
                      
    header("Location: register.php?error=2");   //Sorry, that username already exists
                   
    }

                   if(
    $password_check == 0){
                      
    header("Location: register.php?error=3"); //The Password and Confirmation Password you entered do not match
                   
    }

                   if(
    $email_check 0){
                      
    header("Location: register.php?error=4"); //sorry that email is taken
                   
    }

                   if(
    $email_check2 == 0){
                      
    header("Location: register.php?error=5"); //The Email and Confirmation Email you entered do not match
                   
    }

                   if(
    $usersite_check 0){
                      
    header("Location: register.php?error=7");   //Sorry, that web url is taken
                   
    }

                   if(
    $usersite_check2 == 0){
                      
    header("Location: register.php?error=8");   //your web url can only contain letters and numbers
                   
    }

                   
    header("Location: register.php?error=9991"); //if somehow they got through those if statements, send em back to register
                   
    die("error, report to webmaster ($site_email)");
              } 
    whenever they input something wrong it goes into the first if statement (with all the ||) if nothing is wrong it skips over the if statement, like it should be.

    see how on the bottom i have the "//if somehow they got through those if statements, send em back to register" part. well when it goes inside this if statement, which means it had to qualify as one of the if statements which are seperated with ||. but it NEVER will go into the ifstatements below it, and always goes to the backup header command i put.

    i can't figure out why it does this, it also skips over them if i place them outside the "mother" if statement

  4. #34
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    I haven't done much php, but looking at the structure of the conditional it's pretty obvious that
    a) the header for error # 9991 will always be sent if any error occurs
    b) up to six additional headers will be sent depending on how many fields are invalid

    it seems to me that you'd only wany to send one header if an error occurs. in that case, why not string together all of the errors and send them as a single header?

    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #35
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    And as a side note, sending relative URLs in the Location header field is a violation of the HTTP spec, even though practically all user agents accept it. (Lynx prints a warning.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #36
    Registered User
    Join Date
    Jul 2004
    Posts
    169
    Quote Originally Posted by Sebastiani
    I haven't done much php, but looking at the structure of the conditional it's pretty obvious that
    a) the header for error # 9991 will always be sent if any error occurs
    b) up to six additional headers will be sent depending on how many fields are invalid

    it seems to me that you'd only wany to send one header if an error occurs. in that case, why not string together all of the errors and send them as a single header?


    that was actually my plan, and thats when i figured out that my registratinon check process was broken.

    actualy as i am typing this now i realised that i don't even need that whole section

  7. #37
    Registered User
    Join Date
    Jul 2004
    Posts
    169
    Quote Originally Posted by CornedBee
    And as a side note, sending relative URLs in the Location header field is a violation of the HTTP spec, even though practically all user agents accept it. (Lynx prints a warning.)
    lynx?

  8. #38
    Cat Lover
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    109
    Quote Originally Posted by MisterSako
    lynx?
    I'd assume he means http://lynx.browser.org/

  9. #39
    Registered User
    Join Date
    Jul 2004
    Posts
    169
    oh i've never heard of it before.

    im thinking the fact it's a text only browser would probally very very few to none visitors are going to see my site with lynx

  10. #40
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Perhaps. But the fact that it's a text browser makes it a great tool to check your site for basic accessibility. It gives you the answer to the question, "Does my site work if someone doesn't have all the niceties of a modern browser?" Think about mobile phones and other simple devices.
    I also just mentioned it because it's picky enough to actually mention your HTTP violation.

    I often use Lynx during setting up my Linux computers, by the way, when I need to check something about hardware or download some file. That's before I get a GUI running, so I can't use anything better.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #41
    Registered User
    Join Date
    Jul 2004
    Posts
    169
    hmm that is a good point. i guess the goal of computer sceince isnt ALWAYS to be lazy :-p

    on another note invovlnig my registration process. it jsut occured to me that if you get a error all the fields will be blank if you inputed some wrong data, which could be annoying to the user if they had to write alot of stuff. they could hit the back button and have their infortmation back, but that seems amatuerish and plus it may not always work because of peoples bowsers and stuff.

    so im thinking, if i send their input values back through from the process.php file to the register.php file using GET so that the value of the fields will be as they last entered them .
    ( value="<?php $username ?>" )

    could this pose any kind of secruity threat? i dont normally like to just print out variables like this without using some kind of checker statement to verify that the user didnt type their own stuff in the address bar. but this is the only thing i can think of doing besides combining the registering and processing functions into one document.

  12. #42
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    besides combining the registering and processing functions into one document.
    Which, slightly modified, is not a bad idea. Basically, you should always separate processing and displaying in the code anyway. If you have both well encapsulated, it's easy to make the two effectively the same, so you can directly reuse the values.

    That said, there is no security issue with just GET-forwarding the variables - the only problem would be the long querystring, which might be longer than what the server wants to handle. (I think Apache rejects URLs longer than ~1000 characters, perhaps even less.)
    Well, don't forward the requested password, as it would appear in clear text in the URL, and users might not like that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #43
    mov.w #$1337,D0 Jeremy G's Avatar
    Join Date
    Nov 2001
    Posts
    704
    Boy, I wish I had found this thread sooner because then I wouldn't have to make such a huge post. Here it goes:

    Quote Originally Posted by MisterSako
    is there a way to get user input into variables with php using only one file (ie have the form to input the data and process it in one document instead of "POST"ing to a seperate document)
    Yes, infact it's quite common for smaller scripts. Take for instance this bare bone code:
    PHP Code:
    switch($_REQUEST['action']) {
         case 
    "login"
               
    login();
               break;
         case 
    "search":
               
    search();
               break;
         default: 
               
    main_page();
               break;
    }

    function 
    login() { 
        
    // check if $_REQUEST['user'] and $_REQUEST['pass'] is set
        // .. if not display login form and return; 
        // if set, check if this is a valid login (escape input if this is a mysql ceck.
        // $usrname = mysql_real_escape($_REQUEST['user']);
        // $passwrd = mysql_reql_escape($_REQUEST['pass']);
        // $sql = "select * from login_table WHERE user = '$usrname' AND pass = '$passwrd'"; 
        // .. if result is null display bad login message, display login form again and return;
        // if login is good, set session variables, and forward back to the main page.
    }

    function 
    search() {
       
    // check if search input is specified, if not display search form
       // if search is specified display search results return;
    }

    function 
    main_page() { 
        
    // display your main page
        // return;

    All this requires is that in your forms you include a hidden variable called action so you can easily find out what you should be doing in your code. If you didnt specify an action variable you'd have huge if statements check for which variables should be defined. By using a simple action variable you group your variable checks into single sections. Nice neat code.


    Quote Originally Posted by MisterSako
    another thing im trying to stab at is this fancy mysql database output im trying to do if anyone thinks they could help (im new at php and mysql). Im trying to make my own automatied FAQ system. theres 4 fields for each row but thats not important
    Code:
    $result = mysql_query("SELECT * FROM faq");
                             if (!$result)
                             {
                               $errorreport="3faq printing out faq";
                               echo($errormessage);
                             }
                             else
                             {
                               $highestID=0;
                               $contentSize=0;
                               $contentPrinted=0;
                               while($row = mysql_fetch_array( $result ))
                               {
                                 if($row['ID'] > $highestID)
                                 {
                                   $highestID=$row['ID'];
                                 };
                                 $contentSize=$contentSize+1;
    
                               }
                                
                               echo($highestID);
    
                               while($row = mysql_fetch_array( $result ))
                               {
                                 echo "<p>Question: ";
                                 echo $row['Question'];
                                 echo "<br>Answer: ";
                                 echo $row['Answer'];
                                 echo "</p><br>";
                               }
    the problem is it only processes the first "while($row = mysql_fetch_array( $result ))" part correctly, the 2nd one doesnt do anything. if i switch them around in order the new first one works but not the 2nd one.
    i think the problem is it doesn't look down through each row starting from the begining on the 2nd while loop, is there some kind of command to "reset" the "cursor"?
    The problem with your second loop not working is that you've already looped through all the record sets. Mysql queries return a record set just like an associative array.
    The code:
    PHP Code:
     while($row mysql_fetch_array$result )) 
    Says logically while mysql returns me a record set I want to do this loop. I dont want to exit until fetch_array is null. Hence you come out of the first loop already at the end of your array. Luckily, mysql has a handy dandy function that allows you to reset the record set index. It's mysql_data_seek. However, you should rewrite the code so you don't need to do this as its very unefficient and theres probably a tremendously better way to do it. It looks roughly like you want to know how many QnA's you selected. So you can use mysql_num_rows() To find out.


    Quote Originally Posted by MisterSako
    agh what am i doing wrong???

    http://mrsako.gotdns.com/backupwithlogin.php

    right below the body tag i tried to do this

    <?php
    include("loginsystem/include/session.php");
    ?>

    why does it just write it all to the screen? on my faq section (scroll super alot down and click FAQ) i have php embded within my page there (but not a include file) and it works fine so i know i have PHP and mysql installed correcctly. this is whats in the session.php file
    i tried putting php after <? but it just made the entire site not show up except for the include stuff
    PHP Code:
    editi took this code out because it was a waste of 200 lines 
    btw shadow if i'd still muchly appreciate that zip file :}
    In the php.ini file there's some php tag options. One is asp tags, the other is short tags (<? ?>).
    Quote Originally Posted by php.net
    short_open_tag "1" PHP_INI_PERDIR PHP_INI_ALL in PHP <= 4.0.0.
    asp_tags "0" PHP_INI_PERDIR PHP_INI_ALL in PHP <= 4.0.0.


    Quote Originally Posted by MisterSako
    alright im stuck once again. i started all over and ditched that other guys login system

    i have finished making my own system in which you can login and register
    ( http://mrsako.gotdns.com/index.php )
    its pretty nifty you can even try it if youd like.

    how it works lets say your logging in you type in the information on login.php and you hit submit and it gets processed in a different .php file. and then it sends you back to the site after logging you in. or it sends you back to login.php if you had a wrong username or password. what i want to do is be able to have some text coming up saying "invalid username or password" is there a way i can do this with variables by having the processing php file assign $errormsg="bad uname or pass"; then have login.php echo(errormsg);
    or do i have to do something like login.php?error=1
    im not going to use cookies though thats just dumb, id rather do the login.php?error=1 method
    The customary method is to stay at the login page on error. Ie if the login is being processed on a seperate file, you could also have the same login form in that page so you can echo out an error and display the login again. You could also use session variables even though the user hasn't logged in yet. Just set $_SESSION['login_error_msg'] = "you failed at loggin in"; Then on the main page check to see if the variable is set, if so display the message.

    Quote Originally Posted by MisterSako
    yeh i use cookies to keep track if their logged in or out. but im trying to avoid cookies becasue not everyone has them enabled and you can just edit your cookie file to do stuff that maybe your not supposed to etc.

    i decided to use the assiging variables at the end of the address way for the error reporting. my only slight concern is some browsers might trip up wth stuff like ! : and , being in the address
    Session's are the standard form of variable storage. To keep things secure, the onlything that should be stored in a cookie, is a session id. If they edit the value, then all they do is lose their session.

    Quote Originally Posted by MisterSako
    some people do make random passwords. my passwords never form words, it would be more fool proof. but none of that is important

    whats the command in php to check a string variable for a certain string of text within it.

    ie i have a this variable $dir which is a location to the directory your browsing (im making a online web site control panel thing like alot of free web hosts have) and i want to say like

    if in $dir theres the string "hosted/../" (spelled like that in that order) to change $dir to = "hosted"

    i searched the php website i cant find the command for this. im using the latest version of php (im trying to make it so the user cant browse into directories hes not supposed to be in)
    Use strpos to find the location of a string within a string. If the string does not contain the search, it returns false. In php to determine the difference between false and 0 (because a sub string could start @ 0) use !== or ===. The three character signs mean check variable type as well as value.

    Quote Originally Posted by MisterSako
    also is there a command to remove the last certain amount of characters from a string

    ie
    PHP Code:
    $oldstring="tenletters";

    $newstring=takeofflast($oldstring7);

    if(
    $newstring=="ten")
    {
    echo(
    "it worked");

    actualy does the double quotes make it an array of characters not a string?
    Not a command persay, but very easy to accomplish.
    PHP Code:
    $newstring substr($oldstringstrlen($oldstring)-(8), 7); 
    use -8, because character indexes are 0 based, but strlen is 1 based.

    Quote Originally Posted by MisterSako
    ok another command question

    i know stristr(); can find the first occurance of a character within a string. but how about the last occurance?

    ie i have $dir=hosted/blah/blah2

    i need it to seperate off blah2 by only looking at the string starting after the last /
    This is a fun one, as it involves a neat logic trick.
    PHP Code:
    $search_str strrev($str);
    $needle_str strrev($needle);
    $last_pos strpos($needle_str$search_str) - (strlen($search_str) - strlen($needle_str));
    //                 (13)
    Hello WorldWorld// 18 long
    //    (5)
    !dlroW !dlroW olleH // 18 long
    // 18 - 5 = 13 // huzah 
    Quote Originally Posted by MisterSako
    i have this part in my code. which DID work then out of nowhere it stopped working and i cant understand it. this is apart of my registration process to make sure the user input the right kind of information

    PHP Code:
    if(($username_check 0) || ($usersite_check 0) || ($usersite_check2==0) || ($password_check==0) || ($email_check2==0))
              {

                   if(
    $username_check 0){
                      
    header("Location: register.php?error=2");   //Sorry, that username already exists
                   
    }

                   if(
    $password_check == 0){
                      
    header("Location: register.php?error=3"); //The Password and Confirmation Password you entered do not match
                   
    }

                   if(
    $email_check 0){
                      
    header("Location: register.php?error=4"); //sorry that email is taken
                   
    }

                   if(
    $email_check2 == 0){
                      
    header("Location: register.php?error=5"); //The Email and Confirmation Email you entered do not match
                   
    }

                   if(
    $usersite_check 0){
                      
    header("Location: register.php?error=7");   //Sorry, that web url is taken
                   
    }

                   if(
    $usersite_check2 == 0){
                      
    header("Location: register.php?error=8");   //your web url can only contain letters and numbers
                   
    }

                   
    header("Location: register.php?error=9991"); //if somehow they got through those if statements, send em back to register
                   
    die("error, report to webmaster ($site_email)");
              } 
    whenever they input something wrong it goes into the first if statement (with all the ||) if nothing is wrong it skips over the if statement, like it should be.

    see how on the bottom i have the "//if somehow they got through those if statements, send em back to register" part. well when it goes inside this if statement, which means it had to qualify as one of the if statements which are seperated with ||. but it NEVER will go into the ifstatements below it, and always goes to the backup header command i put.

    i can't figure out why it does this, it also skips over them if i place them outside the "mother" if statement
    Welcome to the Repeating Department of Redundancy Department (RDR Department).

    Chop off the first if statement and just use the nested statements. Also, if you are using cookies, you should be aware that header() tends not to work as session_start() sends out header statements that will make the header() function error out. Use a javascript, or html meta redirect instead unless you know your session isn't going ot use cookies and send out headers before hand.


    Quote Originally Posted by CornedBee
    Which, slightly modified, is not a bad idea. Basically, you should always separate processing and displaying in the code anyway. If you have both well encapsulated, it's easy to make the two effectively the same, so you can directly reuse the values.

    That said, there is no security issue with just GET-forwarding the variables - the only problem would be the long querystring, which might be longer than what the server wants to handle. (I think Apache rejects URLs longer than ~1000 characters, perhaps even less.)
    Well, don't forward the requested password, as it would appear in clear text in the URL, and users might not like that.
    Just conformation here:
    http://www.zend.com/zend/spotlight/mimocsumissions.php

    Quote Originally Posted by Zend
    The second of the restrictions on the GET method leads me to the focus of today's column. Although the GET method is very useful, it has a limit of approximately 1k (1024 characters). This means that any submission that can possibly be greater than 1024 characters (including the names of all of the variables, ampersands, etc.) must be done through the POST method, which will discuss next.
    c++->visualc++->directx->opengl->c++;
    (it should be realized my posts are all in a light hearted manner. And should not be taken offense to.)

  14. #44
    Registered User
    Join Date
    Jul 2004
    Posts
    169
    i can't seem to figure out how to send POSTs between pages without it being a form. can someone give me an example of a variable being sent to a page using the POST method (with out it behing a form)

    i would appreciate it

  15. #45
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by MisterSako
    i can't seem to figure out how to send POSTs between pages without it being a form. can someone give me an example of a variable being sent to a page using the POST method (with out it behing a form)

    i would appreciate it
    To my experience, it's not possible. Did you use the following, by the way? I don't see any disadvantage of using it.
    Code:
    <form ...>
    <input type="hidden" name="someName" value="someVal" />
    ...
    </form>
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. combining c php and mysql
    By Thoth in forum C Programming
    Replies: 2
    Last Post: 01-30-2009, 10:55 AM
  2. PHP installation
    By ssharish2005 in forum Tech Board
    Replies: 8
    Last Post: 11-23-2007, 09:42 PM
  3. PHP on my Computer!
    By xxxrugby in forum Tech Board
    Replies: 4
    Last Post: 03-15-2005, 09:34 AM
  4. C++ and PHP communication
    By Korhedron in forum Game Programming
    Replies: 4
    Last Post: 01-12-2004, 06:37 AM
  5. PHP 4.3.0 released
    By codingmaster in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-30-2002, 07:40 AM