Jump to content

The Bat

Members
  • Posts

    32
  • Joined

  • Last visited

    Never

Posts posted by The Bat

  1. Hi everyone, my PHP skills are a bit rusty, so...

     

    I'm using

    basename($_SERVER['PHP_SELF'])

    to get (for example) blahblah_blah_stuff.php.

     

    What would be the best way to extract just the word "stuff" (the word right before ".php" and right after the last underscore) from it?

     

  2. Hello,

     

    I find myself using quite a bit of (example):

     

    function say_greeting($greeting = null) {
       $greeting = ($greeting) ? $greeting : 'Hello!'
       echo $greeting;
    }
    

     

    in my code (return $greeting if set, otherwise return 'Hello!'), and it's certainly not very DRY. In Ruby, a simple:

     

    def say_greeting(greeting = nil)
       print greeting || 'Hello!'
    end
    

     

    does the trick. I'm wondering if I can do something similar in PHP instead of what I am doing? Thanks!

  3. If I understand what you're talking about (fetching records from a table), then I think this is what you want...

     

    $query = mysql_query("SELECT * FROM stuff ORDER BY 'id'");
    
    while ($row = mysql_fetch_array($query)) {
      echo $row['column_name'];
      echo $row['different_column_name'];
      # etc...
    }
    

     

    Much easier :)

  4. I have an .htaccess file which contains RewriteRule and RewriteCond statements. Here is the code (snippets are examples):

     

    RewriteCond %{REQUEST_URI} /cat [OR]
    RewriteRule ^([^/\.]*)/?$ cat.php?kitty=$1 [L]
    

     

    That way when the URL is mysite.com/cat/foo, foo is kitty. The problem is that "kitty" is applied to all pages. I tried adding

     

    RewriteCond %{REQUEST_URI} !(/cat) [OR]
    RewriteRule ^([^/\.]*)/?$ index.php?something=$1 [L]
    

     

    So that if the URL does not end with /cat, "kitty" will be "something". But this does not work. How can I fix this? Thank you!

  5. Hello,

     

    Is it at all possible in PHP to have "flexible" functions? For example, there's a function which has 3 parameters, and I only want to utilize 2 - the first one and last one. Instead of typing

     

    myFunc('first', false, 'third');
    

     

    Could I/how would I do this?

     

    myFunc('first', $thirdparam = 'third');
    

     

    (Assuming $thirdparam is the same variable used to declare the third parameter when creating the function.)

     

    Using Rails has had me wanting this functionality in PHP ;) Thanks for the help!

  6. But is there any extremely large disadvantages? If a MySql database has a lot of information, won't it load slowly as well?

     

    Not necessarily. If you use a RDMS, and you only need to call rows with a certain condition (like this)

     

    mysql_query("SELECT * FROM mytable WHERE myfield = 'something'");
    

     

    and if it returns, say, 3 rows - that will take no time at all. However, when using a flat file, you'd need to load the whole file first before calling whatever information. Plus, it is extremely easy to work with MySQL in PHP.

  7. say a nonmember knows a page with in the site that should be restricted ex. main.php and they enter that in as the url ex. www.site.com/main.php what restricts them from viewing it?

     

    On every page you want to restrict, you can put

     

    <?php if (!isset($_SESSION['sessionname']) { header('location: index.php'); } ?>
    

     

    at the very top of the page. If there is no session, it redirects to index.php (you can also change index.php to whatever you want, like a page saying they were trying to access a restricted section of the site).

  8. Hello,

     

    I'm not that good in JavaScript, so bare with me. In an external JS file, I have the following function:

     

    function buttonDown(obj) {
       obj.style.backgroundImage = 'url(\'images/button-down.png\')';
    }
    

     

    And then I have an onClick attribute applied to a div:

     

    <a onclick="buttonDown(this)"><div id="button"></a>
    

     

    This isn't working for some reason... but when I put the function code into the onClick attribute itself, it does work. Thank you for your help!

  9. Someone posted a comment saying "[..] but your attitude not to close td and tr tags drives me crazy :D" and the video poster replied "lol, thanks, yeah, I am not that strict on ending tags". That right there is a huge hint not follow any of his techniques. And he sounds 13 ._.

     

    Thank you, thorpe, for that link! I have not known about until now. Very good resource.

     

    P.S.: My squal.

  10. Hey there,

     

    I know how to authenticate users and all that (get matches from a database, if there's a match set a cookie, etc. etc.), but I'm wondering if there's a better, more secure way to validate a user other than a cookie. Currently, the best way I know how to validate someone is to store someone's username in a cookie, and get their info by the SQL statement below:

     

    mysql_query("SELECT * FROM users WHERE username='".$_COOKIE['username']."');
    

     

    Which, I can assume is not that secure.

     

    Yes, I know all about sessions, and I would much rather use them, but the only thing not wanting me to is the abililty (or lack thereof) for sessions to expire at the time I specify (for example when a user clicks 'Remember Me' during login). Or do I just not know about session expire time?

     

    I've heard of someone saying to store the cookie contents into a session, and just use all the sessions in the queries and what not, but can't a user just edit the cookie value thus making a session the same value? (A user can edit cookie values, right?)

     

    I'm looking forward to all of your information, and thanks for the help.

  11. Hmm,

     

    I copied your code and modified it to work on my webserver. First, I gave the submit button a name, and then added an if statement around move_uploaded_file so that it only tries to upload when the submit button is clicked. Also, did you chmod your images folder to 777?

     

    Here is the new code:

    <form action="upload.php" method = "post" enctype = "multipart/form-data">
    
           Photographer name: <input type="text" name="authorname" size="40" />      
    
           Upload File: <input type="file" name="filename" id = "filename" size="40" />     
    
           File Type: <select name = "select">
                      <option value = "JPEG">JPEG</option>
                      <option value = "GIF">GIF</option>
                      <option value = "PNG">PNG</option>
                      <option value = "other">Other</option>
                      </select>
                 
    
           Photo Name: <input type = "text" name = "photoname" />           
    
           Photo Description: <input type = "text" name = "photodescription" />
                                                                     
    
    
           <input name="submit" type="submit" value = "submit"/>
            <input type="reset" value = "reset"/>
    
    
        </form>
        
    <?php
    include("connect.php");
    $path = "/images/";
    $authname = $_POST['authorname'];
    $phototype = $_POST['select'];
    $photoitself = $_POST['filename'];
    $photoname = $_POST['photoname'];
    $photodesc = $_POST['photodescription'];
    
    if ($_POST['submit']) {
       if (move_uploaded_file($_FILES['filename']['tmp_name'], $path.$_FILES['filename']['name'])) {
          echo 'Image uploaded!';
       } else {
          echo 'Sorry, your image wasn\'t uploaded. Upload error: '.$_FILES['filename']['error'];
       }
    }
    ?>
    

     

    That should do it for you. Good luck!

  12. It seems like you're nesting 2 of the same things ($result), and could be better typed like this:

     

    <?php
    if ($result = mysql_query("SELECT username,password FROM login WHERE username = '$emailID' and userpassword='$password'")) {
      $flag = 1;
    } else {
      $flag = 0;
    }
    ?>
    

     

    Try that, and see how it works.

  13. You should change this line:

    move_uploaded_file($files['filename']['tmp_name'],"/public_html/images/" . $_FILES['filename']['name']);
    

    to:

    move_uploaded_file($_FILES['filename']['tmp_name'],"/public_html/images/" . $_FILES['filename']['name']);
    

     

    If that doesn't help, you could change the above to:

     

    if (move_uploaded_file($_FILES['filename']['tmp_name'],"/public_html/images/" . $_FILES['filename']['name'])) {
      echo 'Image uploaded!';
    } else {
      echo 'Error: '.$_FILES['filename']['error'];
    }
    

     

    And then look up the error code here: http://us3.php.net/manual/en/features.file-upload.errors.php

     

    Also, is addphoto3.php the PHP code you posted? If so, then you should remove '/public_html/' from the above line.

  14. The random characters in the session is the encrypted value of it. To display the un-encrypted value, just do

    <?php
    echo $_SESSION['name'];
    ?>
    

    So if $name was, for instance, 'Andrew', then the above code would display 'Andrew'.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.