Trang 1 / 22 12346 ... LastLast
Hiển thị kết quả từ 1 đến 10 / 216
  1. #1
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts

    [PHP Develop]Một ngày một kinh nghiệm !

    Tự tạo bộ đếm cho website của bạn !
    Yêu cầu host hổ trợ PHP

    B1: Tạo 1 file với tên counterlog.txt

    B2: Tiếp theo tạo thêm 1 file counter.php có nội dung như sau:

    PHP Code:
    <?php 
    $viewss 
    file("counterlog.txt"); 
    $views $viewss***91;0***93;; $views++; 
    $fp fopen("counterlog.txt""w"); 
    fwrite($fp$views); 
    fclose($fp); 
    ?>
    Bạn up 2 file này lên host, bạn CHMOD file counterlog.txt là 777. Để hiển thị bộ đếm này trên trang web bạn chỉ cần khai báo dòng này vào đầu trang là được:

    PHP Code:
    <?php 
    include ("counter.php"); 
    ?>
    Đây là đoạn mã hiển thị bộ đếm trên site, đặt nó vào đâu thì tùy thuộc vào bạn:
    PHP Code:
    <?php 
    print $views
    ?>
    Quote Quote


  2. #2
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    Kiểm tra đăng nhập và password.

    Viết 1 form HTML với nội dung sau:

    PHP Code:
    <form method="POST" action="check.php"
    <
    div align="left"><p><font face="BankGothic Md BT">Name</font
    <
    input type="text" name="name" size="14"
    <
    br
    <
    div align="left"><p><font face="BankGothic Md BT">Password</font
    <
    input type="password" name="pw" size="14"><BR><BR><input type="submit" 
    value="Submit"></p
    </
    div></form
    Viết 1 file check.php có nội dung sau:
    PHP Code:
    <? 
    $guestpass = "lightswitch"; 
    if ($name == "Guest" || "guest") 

      if ($pw == $guestpass) 
      { 
        echo "Welcome to the members area $name !"; 
      } 

    else 

      echo "Wrong password"; 

    ?>
    Chúc bạn thành công !

  3. 3 thành viên Like bài viết này:


  4. #3
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    [/b]Random Password[/b]

    PHP Code:
    <?php
    $min 
    4
    // độ dài pass tối thiểu
    $max 15;
    // độ dài pass lớn nhất
    $pwd "";
    // khoảng để hiển thị pass

    for($i 0$i rand($min$max); $i++) {
    $num rand(48122);

    if ((
    $num 97 && $num 122)) {
    $pwd .= chr($num);
    }

    else if((
    $num 65 && $num 90)) {
    $pwd .= chr($num);
    }

    else if((
    $num 48 && $num 57)) {
    $pwd .= chr($num);
    }

    else if(
    $num == 95) {
    $pwd .= chr($num);
    }

    else
    {
    $i--;
    }
    }

    echo 
    $pwd
    // Hiển thị pass
    ?>

  5. 5 thành viên Like bài viết này:


  6. #4
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    Tạo 1 cái Shoutbox đơn giản

    Bạn làm theo các bước sau đây.

    B1: Tạo 1 DB như sau:

    PHP Code:
    CREATE TABLE `tagboard` (
    `
    idint(7NOT NULL auto_increment,
    `
    namevarchar(250NOT NULL default '',
    `
    commentstext NOT NULL,
    `
    datetimestamp(14NOT NULL,
    PRIMARY KEY (`id`),
    TYPE=MyISAM AUTO_INCREMENT=419 
    B2:Viết 1 file config.php có nội dung như sau:

    PHP Code:
    <?
    $dbuser=""; //Database Username
    $dbname=""; //Database Name
    $dbpass=""; // Database Password
    ?>
    B3:Tạo 1 file tag.php có nôi dung như sau:

    PHP Code:
    <iframe src="view.php" name="tag" width="179" height="130" frameborder=0 marginwidth="0" marginheight="0"></iframe><form method="POST" ACTION="todo.php">
    <
    input type="text" NAME="name" Value=">">
    <
    textarea NAME="post" cols="27" rows="3"></textarea>
    <
    br>
    <
    input TYPE="submit" value="Submit"
    B4:Tiếp theo là file todo.php

    PHP Code:
    <?php
    if ($name == '' || $post == ''")
    {
    die ("
    Please fill all fieldsClick <a HREF=tag.php>here</ato return.");
    }
    include('config.php');

    $post = preg_replace("/</","&lt;",$post);
    $post = preg_replace("/>/","&gt;",$post);
    $post = nl2br($post);
    $comments = "$post";
    }

    $c=mysql_connect("localhost","$dbuser","$dbpass");
    mysql_select_db(
    $dbname);
    $todo="INSERT INTO tagboard (id,name,comments,dateVALUES('','$name','$comments',now())";
    $solution = mysql_query($todo) or die (mysql_error());
    if (
    $solution)
    {
    ?>
    <meta http-equiv="
    refresh" content="0;url=tag.php" >
    <?
    } ?>
    B5:Cuối cùng là file view.php

    PHP Code:
    <?php
    include('config.php');
    //Kết nối DB
    $c=mysql_connect("localhost","$dbuser","$dbpass");
    //Lựa chọn DB
    mysql_select_db($dbname);
    //Lựa chọn tên DB
    $todo='SELECT * FROM `tagboard` order by id desc LIMIT 50';
    $solution=mysql_query($todo);
    while (
    $place mysql_fetch_array($solution))
    {
    $id=$place***91;"id"***93;;
    $comments=$place***91;"comments"***93;;
    //là nơi hiển thị trên trang web
    ?>
    »<b><? echo "$place***91;name***93;"; ?></b><? echo ": $comments<br>" ; ?>
    <?
    }?>
    Chúc bạn thành công !

  7. 4 thành viên Like bài viết này:


  8. #5
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    Tạo 1 mini chat đơn giản.

    B1. Tạo 1 file HTML có nội dung như sau:
    PHP Code:
    <html>
    <
    head>
    <
    title>Mini chat đơn giản</title>
    </
    head>
    <
    body>
      <
    form method="post"><br>
      
    Login:<input type="text" name="login" size="6"><br>
      
    Message:<input type="text" name="message" size="10"><br>
      <
    input type="submit" value="Send"><br>
      </
    form>
    </
    body>
    </
    html
    B2. Tạo 1 file php có nội dung như sau
    PHP Code:
    <?php
    /*
    CREATE TABLE MINICHAT (
      LOGIN varchar(20) NOT NULL default '',
      MESSAGE varchar(255) NOT NULL default '',
      ITSTIME varchar(10) NOT NULL default ''
    );
    */

    // Thêm thông điệp vào mini chat
    function addMessage$login$message ) {

       
    $login mysql_escape_stringstrip_tags$login ) );
       
    $message mysql_escape_stringstrip_tags$message'<a><b><i><u>') );
       
    mysql_query"INSERT INTO MINICHAT ( ITSTIME, LOGIN, MESSAGE ) VALUES ( "'".time()."''".$login."','".$message."' )");
    }

    // Gửi thông điệp
    if ( isset( 
    $_POST***91;'msg'***93; )) {
       addMessage( 
    $_POST***91;'login'***93;, $_POST***91;'msg'***93; );
    }
    ?>
    Chú ý: Bạn nên tách rời đoạn tạo DB va lưu nó thành 1 file config.php, bạn tham khảo thử bài trên.

    Chúc bạn thành công !

  9. 7 thành viên Like bài viết này:


  10. #6
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    Viết 1 trang Upload = PHP.

    B1. Bạn viết 1 trang HTML có nội dung như sau:

    PHP Code:
    <html
    <
    head
    <
    title>Upload</title
    </
    head
    <
    body
    <
    h1>Upload</h1
    <
    form enctype="multipart/form-data" action="upload.php" method="post"
    <
    input type="hidden" name="MAX_FILE_SIZE" value="1000000"File
    <
    input name="userfile" type="file"
    <
    input type="submit" value="Upload"
    </
    form
    </
    body
    </
    html

    B2. Bạn viết 1 trang upload.php:

    PHP Code:
    <?php
    // $userfile is where file went on webserver 
    $userfile $HTTP_POST_FILES***91;'userfile'***93;***91;'tmp_name'***93;; 
    // $userfile_name is original file name 
    $userfile_name $HTTP_POST_FILES***91;'userfile'***93;***91;'name'***93;;
    // $userfile_size is size in bytes 
    $userfile_size $HTTP_POST_FILES***91;'userfile'***93;***91;'size'***93;; 
    // $userfile_type is mime type e.g. image/gif 
    $userfile_type $HTTP_POST_FILES***91;'userfile'***93;***91;'type'***93;; 
    // $userfile_error is any error encountered 
    $userfile_error $HTTP_POST_FILES***91;'userfile'***93;***91;'error'***93;; 

    // userfile_error was introduced at PHP 4.2.0 
    // use this code with newer versions 

    if ($userfile_error 0) { 
    echo 
    'Problem: '
    switch (
    $userfile_error
    { case 
    1
    echo 
    'File exceeded upload_max_filesize'
    break; 
    case 
    2
    echo 
    'File exceeded max_file_size'
    break; 
    case 
    3
    echo 
    'File only partially uploaded'
    break;
    case 
    4
    echo 
    'No file uploaded'
    break; 

    exit; 


    // put the file where we'd like it 
    $upfile '/uploads/'.$userfile_name

    // is_uploaded_file and move_uploaded_file 
    if (is_uploaded_file($userfile)) 

    if (!
    move_uploaded_file($userfile$upfile)) 

    echo 
    'Problem: Could not move file to destination directory'
    exit; 

    } else { 
    echo 
    'Problem: Possible file upload attack. Filename: '.$userfile_name
    exit; 

    echo 
    'File uploaded successfully<br /><br />'

    // show what was uploaded 
    echo 'Preview of uploaded file contents:<br /><hr />'
    echo 
    $contents;
    echo 
    '<br /><hr />'
    ?>
    Chú ý ở trên: dòng $upfile = '/uploads/'.$userfile_name; là thư mục các file upload được lưu vào, bạn có thể thay đổi nó tùy theo ý mình.

    Chúc bạn thành công !

  11. 3 thành viên Like bài viết này:


  12. #7
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    Một dạng Upload file khác, tùy chọn file mà bạn muốn Upload.

    Bạn viết 1 file php có nội dung như sau:

    PHP Code:
    <?php 
      
    //Define some variables 
          
    $dir "path/where/you/want/to/upload/files/"//Bạn nên thay đổi đường dẫn cho phù hợp
        //Kiều file, Gif, jpeg, zip ::bạn có thể sửa đổi nếu thích
          
    $types = array("image/gif","image/pjpeg","application/x-zip-compressed"); 
        
    //Check to determine if the submit button has been pressed 
        
    if(isset($_POST***91;'submit'***93;)){ 

    //Shorten Variables 
         
    $tmp_name $_FILES***91;'upload'***93;***91;'tmp_name'***93;; 
         
    $new_name $_FILES***91;'upload'***93;***91;'name'***93;; 

    //Check MIME Type 
        
    if (in_array($_FILES***91;'upload'***93;***91;'type'***93;, $types)){ 
                       
             
    //Move file from tmp dir to new location 

            
    move_uploaded_file($tmp_name,$dir $new_name); 
              
            echo 
    "{$_FILES***91;'upload'***93;***91;'name'***93;} was uploaded sucessfully!";                                        
              
        }else{ 
                   
        
    //Print Error Message 

         
    echo "<small>File <strong><em>{$_FILES***91;'upload'***93;***91;'name'***93;}</em></strong> Was Not Uploaded!</small><br />"
         
        
    //Debug 
       
    $name =  $_FILES***91;'upload'***93;***91;'name'***93;; 
       
    $type =    $_FILES***91;'upload'***93;***91;'type'***93;; 
       
    $size =    $_FILES***91;'upload'***93;***91;'size'***93;; 
       
    $tmp =     $_FILES***91;'upload'***93;***91;'name'***93;; 
        
       echo 
    "Name: $name<br/ >Type: $type<br />Size: $size<br />Tmp: $tmp"
                 
        } 
          
        }      
          
    else{      
          
        echo 
    'Could Not Upload Files'
          

            
    ?> 
              
        <form action="<?php echo $_SERVER***91;'PHP_SELF'***93;; ?>" method="post" enctype="multipart/form-data"> 
          
          <fieldset> 
                  <legend>Upload Files</legend> 
                                              
              <input type="file" name="upload" /> 
    </fieldset> 
    <input type="submit" name="submit" value="Upload Files" /> 
    </form>
    Chúc các bạn thành công !

  13. 4 thành viên Like bài viết này:


  14. #8
    Tham gia
    05-06-2004
    Bài viết
    40
    Like
    0
    Thanked 6 Times in 3 Posts

    Tham khao PHP & MYSQL

    Nhung doan code nay rat can cho nhung ai hoc PHP cac ban tham khao nhe tuyet voi day! chuc cac ban thanh cong
    <?php

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function checkDBConnection() {
    $dblink = mysql_connect("localhost", "sa");
    mysql_select_db("catalog", $dblink);
    return $dblink;
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function releaseConnection($dblink) {
    mysql_close($dblink);
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function getNavigationProperties (&$arrParams) {
    $link = checkDBConnection();

    $arrParams = array();
    $sql = "select value from systeminfo where name='NAV_PROPERTY'";
    $queryres = mysql_query($sql, $link);
    if (!$queryres) {
    //processes SQL failed
    echo mysql_error($link);
    return;
    }

    while ($row = mysql_fetch_array($queryres)) {
    $arrParams[] = $row['value'];
    }
    mysql_free_result($queryres);

    releaseConnection($link);
    }
    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function getItemProperties (&$arrParams) {
    $link = checkDBConnection();
    $arrParams = array();
    $sql = "select value from systeminfo where name = 'ITEM_PROPERTY'";
    $queryres = mysql_query($sql, $link);
    if (!$queryres) {
    echo mysql_error($link);
    return;
    }
    while ($row = mysql_fetch_array($queryres)) {
    $arrParams[] = $row['value'];
    }
    mysql_free_result($queryres);
    releaseConnection($link);
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function ecx_addNavigationProperties ($arrParams, &$bSuccess) {
    $link = checkDBConnection();

    $sql = "lock tables systeminfo write";
    $queryres = mysql_query($sql, $link);
    $res = '';

    foreach($arrParams as $param) {
    $sql = sprintf("insert into systeminfo values('NAV_PROPERTY','%s')", $param);
    $queryres = mysql_query($sql, $link);
    if (!$queryres) {
    if ($res != '') $res .= ',';
    $res = $res.$param;
    }
    }
    $sql = "unlock tables";
    $queryres = mysql_query($sql, $link);

    releaseConnection($link);

    $doc = new_xmldoc("1.0");
    $root = $doc->add_root("CATALOG");
    $schema = $root->new_child("SCHEMA", '');
    $nav = $schema->new_child("NAVIGATION", '');
    $nv = $nav->new_child("NAMEVALUE", '');
    $nv->new_child("NAME", "RESULT");
    $nv->new_child("VALUE", ($res == '')? "SUCCESS": $res);

    $bSuccess = TRUE;
    return $doc->dumpmem();
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function ecx_deleteNavigationProperties ($arrParams, &$bSuccess) {
    $link = checkDBConnection();

    $sql = "lock tables systeminfo write";
    $queryres = mysql_query($sql, $link);
    $res = '';

    foreach($arrParams as $param) {
    $sql = sprintf("delete from systeminfo where name='NAV_PROPERTY' and value = '%s'", $param);
    $queryres = mysql_query($sql, $link);
    if (!$queryres) {
    if ($res != '') $res .= ';';
    $res = $res.$param;
    }
    }
    $sql = "unlock tables";
    $queryres = mysql_query($sql, $link);

    releaseConnection($link);

    $doc = new_xmldoc("1.0");
    $root = $doc->add_root("CATALOG");
    $schema = $root->new_child("SCHEMA", '');
    $nav = $schema->new_child("NAVIGATION", '');
    $nv = $nav->new_child("NAMEVALUE", '');
    $nv->new_child("NAME", "RESULT");
    $nv->new_child("VALUE", ($res == '')? "SUCCESS": $res);

    $bSuccess = TRUE;
    return $doc->dumpmem();
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function ecx_getNavigationProperties (&$bSuccess) {
    getNavigationProperties ($params);

    $doc = new_xmldoc("1.0");
    $root = $doc->add_root("CATALOG");
    $schema = $root->new_child("SCHEMA", '');
    $nav = $schema->new_child("NAVIGATION", '');
    foreach($params as $param) {
    $nv = $nav->new_child("NAMEVALUE", '');
    $nv->new_child("NAME", 'NAV_PROPERTY');
    $nv->new_child("VALUE", $param);
    }

    $bSuccess = TRUE;
    return $doc->dumpmem();
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function ecx_addItemProperties ($arrParams, &$bSuccess) {
    print $arrParams;
    $link = checkDBConnection();

    $sql = "lock tables systeminfo write";
    $queryres = mysql_query($sql, $link);
    $res = '';

    foreach($arrParams as $param) {
    $sql = sprintf("insert into systeminfo values('ITEM_PROPERTY','%s')", $param);
    $queryres = mysql_query($sql, $link);
    if (!$queryres) {
    if ($res != '') $res .= ',';
    $res = $res.$param;
    }
    }
    $sql = "unlock tables";
    $queryres = mysql_query($sql, $link);

    releaseConnection($link);

    $doc = new_xmldoc("1.0");
    $root = $doc->add_root("CATALOG");
    $data = $root->new_child("DATA", '');
    $item = $data->new_child("ITEM", '');
    $nv = $item->new_child("NAMEVALUE", '');
    $nv->new_child("NAME", "RESULT");
    $nv->new_child("VALUE", ($res == '')? "SUCCESS": $res);

    $bSuccess = TRUE;
    return $doc->dumpmem();
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function ecx_deleteItemProperties ($arrParams, &$bSuccess) {
    $link = checkDBConnection();

    $sql = "lock tables systeminfo write";
    $queryres = mysql_query($sql, $link);
    $res = '';

    foreach($arrParams as $param) {
    $sql = sprintf("delete from systeminfo where name='ITEM_PROPERTY' and value = '%s'", $param);
    $queryres = mysql_query($sql, $link);
    if (!$queryres) {
    if ($res != '') $res .= ',';
    $res = $res.$param;
    }
    }
    $sql = "unlock tables";
    $queryres = mysql_query($sql, $link);

    releaseConnection($link);

    $doc = new_xmldoc("1.0");
    $root = $doc->add_root("CATALOG");
    $data = $root->new_child("DATA", '');
    $item = $data->new_child("ITEM", '');
    $nv = $item->new_child("NAMEVALUE", '');
    $nv->new_child("NAME", "RESULT");
    $nv->new_child("VALUE", ($res == '')? "SUCCESS": $res);

    $bSuccess = TRUE;
    return $doc->dumpmem();
    }

    //------------------------------------------------------------------------
    //
    //------------------------------------------------------------------------
    function ecx_getItemProperties (&$bSuccess) {
    getItemProperties ($params);

    $doc = new_xmldoc("1.0");
    $root = $doc->add_root("CATALOG");
    $data = $root->new_child("DATA", '');
    $item = $data->new_child("ITEM", '');
    foreach($params as $param) {
    $nv = $item->new_child("NAMEVALUE", '');
    $nv->new_child("NAME", 'ITEM_PROPERTY');
    $nv->new_child("VALUE", $param);
    }

    $bSuccess = TRUE;
    return $doc->dumpmem();
    }

  15. 4 thành viên Like bài viết này:


  16. #9
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    Hiển thị thông tin trình duyệt đang sử dụng.

    Bạn sử dụng đoạn PHP sau trong đoạn mà bạn muốn hiển thị:

    PHP Code:
    <? 
    $browser = "$HTTP_USER_AGENT"; 
    print("Bạn đang sử dụng: $browser")
    ?>
    Để đẹp hơn bạn có thể sửa thành:

    PHP Code:
    print("<font color=\"red\"> Bạn đang sử dụng: $browser <font>"
    Chúc bạn thành công !

  17. 2 thành viên Like bài viết này:


  18. #10
    Tham gia
    21-02-2004
    Bài viết
    195
    Like
    0
    Thanked 58 Times in 17 Posts
    Sử dụng PHP để chuyển đến 1 URL khác

    PHP Code:
    <?php
    header
    ("Location: http://yoursite.com/")
    ?>
    Trong HTML bạn có thể sử dụng đoạn mã sau:

    PHP Code:
    <html>
        <
    head>
        <
    title>Please wait...</title>
        <
    script language="JavaScript">
        
    self.location.href='http://yoursite.com/';
        
    </script>
        </head>
        <body>
        </body>
        </html> 
    Chúc các bạn thành công !

  19. 4 thành viên Like bài viết này:


Trang 1 / 22 12346 ... LastLast

Bookmarks

Quy định

  • Bạn không thể tạo chủ đề mới
  • Bạn không thể trả lời bài viết
  • Bạn không thể gửi file đính kèm
  • Bạn không thể sửa bài viết của mình
  •