Benny's Blog
Navigation: Home » Archives for PHP
22. April 2012

The syntax of JavaScript and PHP is very similar. How similar is shown by the following function that I wrote to generate a random password:

PHP code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
function randomPassword($length){
  $password = "";
  for($i=0; $i<$length; $i++){		
    $func = rand(0,2);
    if($func == 0)
      $password = $password.chr(rand(48,57));
    else if($func == 1)
      $password = $password.chr(rand(65,90));
    else if($func == 2)
      $password = $password.chr(rand(97,122));
  }
  return $password;
}
 
$randomPassword = randomPassword(20);
echo($randomPassword);
?>

JavaScript code:

<script type="text/javascript" line="1">
function rand(min,max){
  if (arguments.length === 0){
    min = 0;
    max = 32767;
  }
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
 
String.prototype.chr = function(code){
  return String.fromCharCode(code);
}
 
function randomPassword($length){
  $password = "";
  for($i=0; $i<$length; $i++){		
    $func = rand(0,2);
    if($func == 0)
      $password += $password.chr(rand(48,57));
    else if($func == 1)
      $password += $password.chr(rand(65,90));
    else if($func == 2)
      $password += $password.chr(rand(97,122));
  }
  return $password;
}
 
$randomPassword = randomPassword(20);
document.write($randomPassword);
</script>
21. April 2012

The lower case letter “a” has ASCII code “97″ and can be generated in PHP like this:

<?php
  $a = chr(97);
  echo $a;
?>

In JavaScript, the same can be accomplished by:

<script type="text/javascript">
  var a = String.fromCharCode(97);
  document.write(a);
</script>

And this is how it works in Java:

public static void main(String[] args){
  String a = new Character((char) 97).toString();
  System.out.println(a);
}
20. April 2012

In PHP the rand() function returns a random number between a given minimum and maximum (by default 0 and 32767). The whole looks like this:

<?php
  $randomNumber = rand(1,100); // random number between 1 and 100
  echo $randomNumber;
?>

The same function can be rebuild with JavaScript:

<script type="text/javascript">
  function rand(min,max){
    if (arguments.length === 0){
      min = 0;
      max = 32767;
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
 
  var randomNumber = rand(1,100); // random number between 1 and 100
  document.write(randomNumber);
</script>
9. April 2012

In PHP it is very easy to print a GET-Parameter. You can do it like this:

<?php
  $value = $_GET["param"];
  echo $value;
?>

In JavaScript it is possible too, but needs some effort:

<script type="text/javascript">
  (function(){
    var s = window.location.search.substring(1).split('&');
    if(!s.length) return;
    window.$_GET = {};
    for(var i  = 0; i < s.length; i++){
      var parts = s[i].split('=');
      window.$_GET[unescape(parts[0])] = unescape(parts[1]);
    }
  }())
 
  var value = $_GET["param"];
  document.write(value);
</script>
22. August 2010

In meinem Beitrag “SVN SSH post-commit checkout hook” habe ich gezeigt, wie man ein Projekt mit SVN automatisch in einem Webverzeichnis auschecken kann. Das dient dazu, um den aktuellen Entwicklungsstand über einen Web-Server verfügbar zu machen.

Damit das auch geht, muss ein virtueller Host definiert werden. Wenn es sich um ein Web-Projekt handelt, dass mit dem Zend Framework erstellt wurde, dann muss man auch noch das Zend Framework auf dem Server installieren.

Wie das geht, zeige ich am Beispiel eines Debian Servers mit einem Apache-Webserver.

…weiterlesen

18. August 2010

Häufig wird in Foren gefragt, wie man mit JavaScript gezielt Daten über einen HTTP-Request an eine bestimmte Datei senden kann. Weil dieser Vorgang in der Web-Entwicklung besonders wichtig ist, habe ich ein kurzes Beispiel programmiert, dass diese Funktionalität besitzt.

…weiterlesen