Jump to content

aHMAD_SQaLli

Members
  • Posts

    36
  • Joined

  • Last visited

Posts posted by aHMAD_SQaLli

  1. hello

    if there is just the first function it run very good but if I add the second I get the message in Firefox " TypeError: event.target is undefined "

    (function(){
    	
    	document.getElementById("ta").onclick = function(){ ta(); };	
    	function ta(){
    			
    			var tav = document.getElementById("ta").value, ta = document.getElementById("ta");
    			ta.setAttribute('style','height:200px;')
    	}
    })();
    window.onclick = function(event) {
    	
    	var tav = document.getElementById("ta").value, ta = document.getElementById("ta");
    	if (! event.target.matches('#ta')) {
    		
    		if( tav == "" ){
    			
    			ta.setAttribute('style','height:25px;');
    		}
    		else if( tav !== "" ){
    			
    			ta.setAttribute('style','height:200px;');
    		}
    	}
    }
    (function(){
    	
    	document.getElementById("close").onclick = function(){hide_notify();};	
    	function hide_notify(){
    		
    		var notify = document.getElementById("notify");
    		notify.parentElement.removeChild(notify);
    	}
    })();
    

    I'm still new in JavaScript so I have no idea what is going on !

    thanks.

  2. In that case, you'll need a very different approach.

     

    When you first check the database and then insert a new row, there's a small timeframe where a different PHP process may also insert a new row and invalidate the result of your check. Your application won't see that and just keeping going. In the worst case, you now have two rows with the same data despite your checks.

     

    This situation is unlikely to happen by accident, but the bug can actively be exploited by anybody.

     

    A much more reliable solution is to let the database do the check. Add a UNIQUE constraint to the columns and then simply try to insert the new row. If that fails due to a constraint violation, you know the data is not unique. Otherwise everything is fine.

    <?php
    
    // database settings
    const DB_HOST = '...';
    const DB_USER = '...';
    const DB_PASSWORD = '...';
    const DB_NAME = '...';
    const DB_CHARSET = 'UTF8';
    
    // MySQL error codes
    const MYSQL_ER_DUP_ENTRY = 1062;
    
    
    
    // Enable exceptions for MySQLi.
    $mysqli_driver = new mysqli_driver();
    $mysqli_driver->report_mode = MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT;
    
    $database_connection = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    
    $database_connection->set_charset(DB_CHARSET);
    
    
    
    $test_name = 'foo';
    $test_email_address = 'bar@example.com';
    
    $user_registration_stmt = $database_connection->prepare('
        INSERT INTO users
        SET
          public_name = ?,
          email_address = ?
    ');
    
    $user_registration_stmt->bind_param('ss', $test_name, $test_email_address);
    
    // Try to insert row.
    $user_registration_errors = [];
    try
    {
        $user_registration_stmt->execute();
    }
    catch (mysqli_sql_exception $user_registration_exception)
    {
        // Was the error caused by a duplicate entry?
        if ($user_registration_exception->getCode() == MYSQL_ER_DUP_ENTRY)
        {
            $user_registration_errors[] = 'The username or e-mail address is already in use.';
        }
        else
        {
            // It's some other problem, pass the exception on.
            throw $user_registration_exception;
        }
    }
    
    if ($user_registration_errors)
    {
        foreach ($user_registration_errors as $error)
        {
            echo htmlspecialchars($error, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8').'<br>';
        }
    }
    else
    {
        echo 'Registration successful!';
    }
    

    There's still a problem left: Usually, the e-mail addresses of users are private, so they must not be exposed, neither directly nor indirectly. Right now, anybody can check if an address is already in your database simply by trying to register with it.

     

    Is that different for your site? Do you have an agreement with your users that all e-mail addresses are public?

     

    thanks for the code !

    and thank you for highlighting this problem, actually I'm still new in PHP/MySQLi and this test website will not be published, it's just for improve my coding skills.

  3. What is the purpose of this check? Is it the usual uniqueness check for a user registration script? In that case, there are much better alternatives.

     

    Also, you generally must not expose the e-mail addresses of your users to the public. Whether a particular address is registered at your site is none of anyone's business. Instead, you would send out a mail telling the user that they already have an account.

     

    I need it for a registration script

  4. Hello

    I'm trying to check if 2 values exist in the database using php, Google didn't help...

     

    I need something like this :

    if($stmt = mysqli_prepare($db_connect,'QUERY TO CHECK IF USERNAME AND EMAIL EXIST')){
    mysqli_stmt_bind_param($stmt, "ss", $user,$email);
    mysqli_stmt_execute($stmt);
    /*
        if username exist
            echo username exist
        if email exist
            echo email exist
    */
    }
    else{/*error*/}
    

    thanks !

  5. Hello

     

    I Googled Responsive vs Adaptive design , and almost all results recommend using Responsive design and say that Adaptive design has a lots Cons.

     

    But... I noticed that famous websites like Google, Facebook, Yahoo, Youtube and many more use Adaptive Design, so I'm wandering Why they use Adaptive design if the majority say that responsive is better ?

    Thanks !

  6. Thanks for the detailed explain it really make more sense, and I know about jQuery but I just prefer to use Javascript because I'm still learning.

    sorry if I'm asking to much but, can you give me a better code example if it's possible, I'll be Very Thankful, still new with JavaScript.

  7. Hello

    I have this Ajax code witch working good, but I'm not really good at javascript I just started 1 month ago, so I'm not sure if this code is perfect or not, or is there any way to improve this AJAX code.

    HTML/AJAX

    <input type="text" id="text_1" />
    <button name="js_btn_func" id="js_btn_func" onclick="ajax()" > Execute JavaScript / AJAX Function </button>
    <div id="success_2"></div>
    <div id="result_2"></div>
    <div id="error_2"></div>
    <script>
    try {
    	var results_area_2 = document.getElementById("result_2");
    	var error_area_2 = document.getElementById("error_2");
    	var success_area_2 = document.getElementById("success_2");
    	function ajax(str) {
    		var xhttp;
    		var str = document.getElementById("text_1").value;
    		if (str == "") {
    			document.getElementById("result_2").innerHTML = "";
    			return;
    		}
    		xhttp = new XMLHttpRequest();
    		xhttp.onreadystatechange = function() {
    			if (xhttp.readyState == 4 && xhttp.status == 200) {
    				results_area_2.innerHTML = xhttp.responseText;
    				success_area_2.innerHTML = ('Success: Script is <b>ON</b>');
    			}
    		}
    		xhttp.open("POST", "action.php?name="+str, true);
    		xhttp.send();
    	}
    }
    catch(e){
        alert('An error has occurred: '+e.message)
    	error_area_2.innerHTML = ('Error: Script is <b>OFF</b>');
    }
    </script>
    

    PHP

    <?php
    $random_query = mt_rand(1, 9999);
    if (isset($_REQUEST["name"])){
    	$q = $_REQUEST["name"];
    	echo "Hello <b>$q</b>! random number : $random_query ";
    }
    
    ?>
    

    Thanks in advance. ::)

  8. Hello

     

    sorry if the English is not good

    Is there any way to get client's timezone or hours difference using PHP and JAVASCRIPT so the time is displayed based on the client's timezone, because I've created a small PHP script and when I upload it to the server, the time was displayed based on the server's timezone.

     

    Thank's in advance, Have a good day.

  9. Hello
    sorry if the question is not proper / clear.
    which one of these two ways is better to validate.

    <?php
    # using a variable
    $query = $_POST['query'];
    $ok = FALSE;
    if ( $query = 'php' ){	$ok = TRUE;}
    else {	$ok = FALSE;}
    if ( $ok = TRUE; ){	// more code
    }
    # direct way
    $query = $_POST['query'];
    if ( $query = 'php' ){	// more code
    }
    else {	// more codes
    }
    ?>
    

    Thanks in advance.

  10. Hello

    I heard that some websites use Java or C++ or Python with PHP in the same time, if some know how it's done please answer these 3 questions:

    1. How can we run PHP and Java or C++ or Python in the same time.

    2. How the variables are passed between them.

    3. Wich one is better, Java or C++ or Python .

     

    Thanks all.

  11. Hello

    sorry for the English if it's bad...

    I'm going to use facebook as an example, if I uploaded my profile picture 1.9Mb 1920x1200, and then I went to the home page, in the top blue navbar I will find that the profile picture size is just 2.24Ko and it's resolution is 36x36.

    so I'm wondering how this can be done using php ? :confused:

     

    Thanks

×
×
  • 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.