12 mar 2008

Walidacja formularza w Javascript

Walidacja formularza w Javascript 

function vf() {

  var a = "";
  var b = "";

  if (document.f2.first_name.value == "") {
    if (!a) b = 'first_name';
    document.f2.first_name.style.background = 'yellow';
    a = a + " - podaj imię"
  }
  else
    document.f2.first_name.style.background = 'white';
  for (i = 1; i <= document.f2.uc.value; i++) {
    t_data_urodzenia_y = eval('document.f2.p' + i + '_dob_yyyy');

    if (t_data_urodzenia_y.selectedIndex == 0) {
      if (!a && t_data_urodzenia_d.selectedIndex == 0) b = 'p' + i + '_dob_yyyy';
      t_data_urodzenia_y.style.background = 'yellow';
      a = a + " - podaj datę urodzenia osoby nr " + i;
    }
    else {
      t_data_urodzenia_y.style.background = 'white';
    }
  }

  var c = document.getElementById('akc_war');
  if (!document.f2.warunki_uczestnictwa.checked) {
    if (!a) b = 'warunki_uczestnictwa';
    c.style.background = 'yellow';
    a = a + " - musisz zaakceptować warunki uczestnictwa";
  }
  else
    c.style.background = 'white';

  if (a) {
    alert("UWAGA!" + a);
    if (b) eval('document.f2.' + b + '.focus()');
    return false;
  }
  return true;
}

Wywołanie walidacji

<form name="f2" action="" onSubmit="return vf()">
9 mar 2008

Podczyt strony ze wskazanego adresu URL, fsockopen

Podczyt strony ze wskazanego adresu URL, fsockopen 

function fsopen($url) {
 if (substr($url, 0, 7) == 'http://')
  $url = substr($url, 7);

 $host = substr($url, 0, strpos($url, '/'));
 $filepath = substr($url, strpos($url, '/'));
 $fp = @fsockopen ($host, 80, $errno, $errstr, 10);</p>

 if (!$fp) {
  $ret_value = "$errstr ($errno)";
 } else {
  $header .= 'GET '.$filepath.' HTTP/1.1'."\r";
  $header .= 'Host: '.$host."\r";
  $header .= 'Connection: close'."\r";
  $header .= "\r";
  fputs ($fp, $header);</p>

  while(!feof($fp)) {
   $line = fgets($fp, 8192);
   if(!$start_data &amp;&amp; preg_match("/^\r$/", $line)) {
    $start_data = 1;
    continue;
   }
   if ($start_data) {
    $ret_value .= $line;
   }
  }

  fclose ($fp);
 }
 return $ret_value;
}
6 mar 2008

Wysyłanie maila w PHP oraz sprawdzenie treści

Funkcja testująca zawartość pól formularza:

function test_content_mail($value) {

    if(eregi('Content-Transfer', $value) || eregi('Content-Type', $value))  
        $value = 'error';

    if(eregi('Subject:', $value) || eregi('bcc:', $value))  
        $value = 'error';

    if(eregi('MIME-Version', $value))
        $value = 'error';       
       
    return $value;
}

Funkcja generująca treść maila: (pola do wysłąnia zaczynaja się od prefiksu 'f_')

    function mail_tresc($data) {

         foreach($data as $key => $value) {
             if(preg_match("/^f_/", $key)) {
                 $error = test_content_mail($value);
                 if($error=='error') {
                     $content='error';
                     break;
                 }
                 $content .= substr($key, 2) . ': ' . $value . "";
             }
         }

         return $content;
    }

    $content = mail_tresc($_REQUEST);
    if($content=='error') {
        echo 'Error... Wprowadzono niedozowlone tresci.';
        exit();
    }

    $to = <a href="mailto:'kody@kody.wig.pl'">'kody@kody.wig.pl'</a>;
    $from = <a href="mailto:'test@kody.wig.pl'">'test@kody.wig.pl'</a>;

    $subject = 'Temat';

Dodatkowe nagłówki maila:

    $headers = "From: $from\r";
    $headers .= "Subject: Temat\r";
    $headers .= "MIME-Version: 1.0\r";      
    $headers .= "Content-Type: text/plain; charset=ISO-8859-2; format=flowed\r";
    $headers .= "Content-Transfer-Encoding: 7bit";
    $body     = 'Treść'."".$content."\r";

    $mail_status = mail($to, $subject, $body, $headers);</span><span class="mce_ws_kod">    $url = $_SERVER['HTTP_REFERER'];
    $pos = strpos($url, '&wyslano');</span><span class="mce_ws_kod">Zabezpiszenie przed doklejaniem w nieskończoność fo referera zmiennej 'wyslano'

    if($pos) {
        $url = substr($url, 0, strpos($url, '&wyslano'));
    }

    header("Location: ".$url.'&wyslano=1');
    exit();
28 lut 2008

Przekierowanie na domenę bez www

Przekierowanie na domenę bez www

RewriteCond %{HTTP_HOST} ^www\.wolakorybutowa\.pl$ [NC]
RewriteRule ^(.*)$ <a href="http://wolakorybutowa.pl/$1">http://wolakorybutowa.pl/$1</a> [R=301,L]