Script

Filter
2004-11-20

Autenticering för apache

Linux
Exempel
1. Skapa först användarna i en fil (utan -c om filen redan finns):
htpasswd -c /abs/sokvag/till/usersfil lasse
htpasswd /abs/sokvag/till/usersfil bosse
htpasswd /abs/sokvag/till/usersfil lena

2. Skapa en fil för grupper och lägg in grupperna (t ex /abs/sokvag/till/groupfil)
medlemmar: lasse bosse lena
pojkar: lasse bosse
flickor: lena

3. I mappen som ska skyddas läggs filen .htaccess:

AuthName "Min sajt"
AuthType Basic
AuthUserFile /abs/sokvag/till/usersfil
AuthGroupFile /abs/sokvag/till/groupfil
Require group medlemmar

(alt Require user lena)
2004-11-13

Avsluta session

Php
Session abandon i php
<?php
session_start();
$_SESSION = array();
session_destroy();
?> 
2004-11-12

Visa tabell

Php
Visar en generell mySQL-tabell oberoende av antalet rader eller kolumner
<html>
<head>
<title>Show table</title>
</head>
<body>
<table border="1">

<?php
$host = "hostname";
$db   = "dbname";
$user = "username";
$pass = "password";
$conn = mysql_connect($host, $user, $pass) or die ("Gick inte att connecta.");
$db = mysql_select_db($db, $conn);

$sql = "select * from table";
$rs = mysql_query($sql, $conn) or die("Kunde inte hämta data.");

print "<tr>";
for ($i = 0; $i < mysql_num_fields($rs); $i++) {
    print "<th>" . mysql_field_name($rs, $i) . "</th>\n";
}
print "</tr>";

while ($row = mysql_fetch_assoc($rs)) {
    print "<tr>";
    foreach($row as $value) {
        print "<td>$value</td>";
    }
    print "</tr>";
}
?>

</table>
</body>
</html> 
2004-11-12

Transaktioner i MySQL

Mysql
Lite tips om vad som krävs...
The ability to group SQL statements into groups that can be committed or rolled back en masse
is only available in the InnoDB table handler in MySQL - MyISAM does not support transactions,
and will silently ignore any transaction-control statements. To get started with a transaction,
send the "BEGIN" SQL statement through mysql_query(), followed by as many statements as you want,
then either "COMMIT" or "ROLLBACK" to save or abort your changes.

Man måste först se till att tabellen är av InnoDB-typ. Man kan blanda MyISAM och InnoDB-typer i en och samma databas.

create table foo (id int) type = InnoDB;

eller

alter table foo type = InnoDB;
2004-11-10

Slumpdatabas

Mysql
Hämta en slumppost från en tabell i en databas
There is a special ordering, RAND(), which returns your rows in a random order.
You can use ORDER BY RAND() LIMIT 1 to pick out one random row from your database.
Thus the following query will put out one random username from the table:
"SELECT Username FROM usertable ORDER BY RAND() LIMIT 1;"
🙂