16
php Basic Functions / PHP Array
« on: November 19, 2010, 12:56:33 PM »Code: [Select]
<?php
$acvalue = array(1 => 'value1',2 => 'value2',3 => 'value3');
echo"<pre>";
print_r ($acvalue);
echo"</pre>";
?>
Output:

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
<?php
$acvalue = array(1 => 'value1',2 => 'value2',3 => 'value3');
echo"<pre>";
print_r ($acvalue);
echo"</pre>";
?>

CREATE TABLE `news` (
`id` int(11) NOT NULL auto_increment,
`title` text collate utf8_unicode_ci,
`text` text collate utf8_unicode_ci,
`date` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=4 ;
INSERT INTO `news` VALUES (1, 'News1', 'Description1', '2010-09-09 16:52:46');
INSERT INTO `news` VALUES (2, 'News2', 'Description2', '2010-09-09 16:54:19');
INSERT INTO `news` VALUES (3, 'News3', 'Description3', '2010-09-09 16:55:11');
<?php
$dbhost = 'localhost';
$dbusername = 'root';
$dbpassword = 'mydbpass';
$dbname = 'test';
$conn=mysql_connect($dbhost, $dbusername, $dbpassword);
if(!$conn) :
die('Could not connect: ' . mysql_error());
endif;
$db=mysql_select_db($dbname, $conn);
if(!$db) :
die ('Can't connect to database : . mysql_error());
endif;
?><?php
//database connection
include_once('config.php');
include('smarty/Smarty.class.php');
$smarty = new smarty();
$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->config_dir = 'config';
$smarty->cache_dir = 'cache';
?>
<?php header('Content-type: application/rss+xml'); ?>
<?php echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n"; ?>
<?php
$res = mysql_query("SELECT * FROM news ORDER by id DESC LIMIT 20")
or die(mysql_error());
while($row = mysql_fetch_array($res)){
$value[] = $row;
}
$smarty->assign('news', $value);
$smarty->caching = 1;
$smarty->display('rss.tpl');
?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>Example.com</title>
<description>Example WebSite</description>
<link>http://www.example.com</link>
{section name=news loop=$news}
<item>
<title>{$news[news].title|stripslashes|truncate:50} :: Example.com</title>
<description>{$news[news].text|stripslashes|escape:'html'}</description>
<link>http://www.example.com/page.php?id={$news[news].id}</link>
<guid>www.example.com/page.php?id={$news[news].id}</guid>
<pubDate>{$news[news].date|date_format:"%a, %e %b %Y %H:%M:%S"} GMT</pubDate>
</item>
{/section}
</channel>
</rss>

<?php
require_once('admin/config.php');
$id = $_GET['id'];
$result = mysql_query("SELECT linkid, text, linkurl, hits FROM oslink WHERE linkid='$id' ")
or die(mysql_error());
while($myrow = mysql_fetch_assoc($result))
{
$resulty = mysql_query("UPDATE oslink SET hits=hits+'1' WHERE linkid='$id'")
or die(mysql_error());
$redirect = $myrow['linkurl'];
?>
<SCRIPT LANGUAGE="JavaScript">
redirTime = "50";
redirURL = "<?php echo $redirect ?>";
function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); }
// End -->
</script>
<BODY onLoad="redirTimer()">
<?php
}
?>
when i visit a page over the script it goes but when i visit a page over the script twice or another link from the page it doesnt work... after a while i wait it goes again.. one visit is ok but the second visit is not going...

<?php
require_once('admin/config.php');
$id = $_GET['id'];
session_start();
if (!isset($_SESSION["jumps"]))
$_SESSION["jumps"] = 0;
$_SESSION["jumps"] = $_SESSION["jumps"] + 1;
if ($_SESSION["jumps"] > 1)
{
//echo "text text";
}
else
{
$result = mysql_query("SELECT linkid, text, linkurl, hits FROM oslink WHERE linkid='$id' ")
or die(mysql_error());
while($myrow = mysql_fetch_assoc($result))
{
$resulty = mysql_query("UPDATE oslink SET hits=hits+'1' WHERE linkid='$id'")
or die(mysql_error());
$redirect = $myrow['linkurl'];
?>
<SCRIPT LANGUAGE="JavaScript">
redirTime = "50";
redirURL = "<?php echo $redirect ?>";
function redirTimer() { self.setTimeout("self.location.href = redirURL;",redirTime); }
// End -->
</script>
<BODY onLoad="redirTimer()">
<?php
}
}
?>
.. second if you put a long group name it is out of the box, any way to fix this?
#aanav{BACKGROUND:#eee;height:22px;width:159px;line-height:22px;list-style:url(images/back.png);text-align:center;border:1px solid #ccc;-moz-border-radius-topleft:12px 12px;-moz-border-radius-topright:12px 12px;-moz-border-radius-bottomleft:12px 12px;-moz-border-radius-bottomright:12px 12px;-webkit-border-top-right-radius:12px;-webkit-border-top-left-radius:12px;-webkit-border-bottom-right-radius:12px;-webkit-border-bottom-left-radius:12px;text-transform:uppercase;}#aanav{BACKGROUND:#eee;width:159px;line-height:22px;list-style:url(images/back.png);text-align:center;border:1px solid #ccc;-moz-border-radius-topleft:12px 12px;-moz-border-radius-topright:12px 12px;-moz-border-radius-bottomleft:12px 12px;-moz-border-radius-bottomright:12px 12px;-webkit-border-top-right-radius:12px;-webkit-border-top-left-radius:12px;-webkit-border-bottom-right-radius:12px;-webkit-border-bottom-left-radius:12px;text-transform:uppercase;}
http://www.example.com/guestbook/
http://www.example.com/fontf/
http://www.example.com/book.php...
http://www.example.com/guestbook/menage/install.php
<IFRAME style="border:1px solid #ffffff" SRC="/book.php" WIDTH=595 HEIGHT=700 BORDER=1 SOLID></iframe>
<?php
////make a MySQL connection
include('config.php');
// smarty configuration
include('Smarty.class.php');
$smarty = new smarty();
$smarty->template_dir = 'templates';
$smarty->compile_dir = 'templates_c';
$smarty->cache_dir = 'cache';
//get ID from URL
$id = $_GET['id'];
//get linkid, text, linkurl from the page table
//..or..*=select everything from the table page
$result = mysql_query("SELECT linkid, text, linkurl
FROM page WHERE linkid='$id' ")
or die(mysql_error());
while($myrow = mysql_fetch_assoc($result))
{
$values[] = $myrow;
}
$smarty->assign('page', $values);
$smarty->caching = 1;
//$REQUEST_URI will create a separate cache file
//for each unique URL when you call webpage.tpl.
$smarty->display('webpage.tpl',$REQUEST_URI);
?>{section name=page loop=$page}
<head><title>{$page[page].text}</title></head>
<h1>{$page[page].text}</h1>
<a href="{$page[page].linkurl}">{$page[page].text}</a>
{/section}
<?php
//MySQL Configuration
//DB Host (Normally 'localhost')
$dbhost = 'localhost';
//DB Database Username
$dbusername = 'root';
//DB Database User Password
$dbpassword = 'mypassword';
//DB Database Name
$dbname = 'mydbname';
//mysql_connect function
$conn=mysql_connect($dbhost, $dbusername, $dbpassword);
if(!$conn) :
die('Could not connect: ' . mysql_error());
endif;
$db=mysql_select_db($dbname, $conn);
if(!$db) :
die ('Cant connect to database : ' . mysql_error());
endif;
?><?php
//make a MySQL connection
include('config.php');
//create a MySQL table in the selected database
mysql_query("CREATE TABLE `page` (
`linkid` int(11) NOT NULL auto_increment,
`text` text collate utf8_unicode_ci,
`linkurl` text collate utf8_unicode_ci,
PRIMARY KEY (`linkid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
COLLATE=utf8_unicode_ci AUTO_INCREMENT=1")
or die(mysql_error());
?><title>Index Page</title>
<center><h2>INDEX</h2></center><BR>
<?php
//make a MySQL connection
include('config.php');
echo " <a href=\"insert.php\">INSERT PHP</a><br>";
//get all the data from the page table
//*=select everything from the table page
$result = mysql_query("SELECT * FROM page")
or die(mysql_error());
while($link=mysql_fetch_array($result)){
//echo (display) a link using $link[linkid]
echo " <a href=\"webpage.php?id=$link[linkid]\">Web Page 1</a><br>";
}
?><title>Insert Page</title>
<center><h2>INSERT PAGE</h2></center><br>
<a href="index.php">INDEX PAGE</a>
<?php
//make a MySQL connection
include('config.php');
//to check if a submit button was clicked, use this...
if(isset($_POST['submit']))
{
$text = $_POST['text'];
$linkurl = $_POST['linkurl'];
//the INSERT INTO statement is used
//to add new records to a database table
mysql_query("INSERT INTO page (text,linkurl)
VALUES ('$text','$linkurl')")
or die(mysql_error());
}else{
?>
<form method="post" action="insert.php">
<TABLE><TR><TD>
Title:
</TD>
<TD>
<input name="text" size="60" maxlength="255">
</TD></TR>
<TR><TD>
URL:
</TD>
<TD>
<input name="linkurl" size="60" maxlength="255">
</TD></TR>
<TR><TD>
<input type="submit" name="submit" value="submit">
</TD></TR></TABLE>
</form>
<?php
}
?>
when i install i get a sql error on line 6 saying;
my database is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' in BLAH BLAH BLAH on line 6
$to = "customer_support@1900tr.com";
$subject = $_REQUEST["subject"];
$body = $_REQUEST["body"];
$email = $_REQUEST["email"];
$name = $_REQUEST["name"];
$dodgy_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"bcc:"
);
function is_valid_email($email) {
return preg_match('#^[a-z0-9.!\#$%&\'*+-/=?^_`{|}~]+@([0-9.]+|([^\s]+\.+[a-z]{2,6}))$#si', $email);
}
function contains_bad_str($str_to_test) {
$bad_strings = array(
"content-type:"
,"mime-version:"
,"multipart/mixed"
,"Content-Transfer-Encoding:"
,"bcc:"
,"cc:"
,"to:"
);
foreach($bad_strings as $bad_string) {
if(eregi($bad_string, strtolower($str_to_test))) {
echo "$bad_string found. Suspected injection attempt - mail not being sent.";
exit;
}
}
}
function contains_newlines($str_to_test) {
if(preg_match("/(%0A|%0D|\\n+|\\r+)/i", $str_to_test) != 0) {
echo "newline found in $str_to_test. Suspected injection attempt - mail not being sent.";
exit;
}
}
if($_SERVER['REQUEST_METHOD'] != "POST"){
echo("Unauthorized attempt to access page.");
exit;
}
if (!is_valid_email($email)) {
echo 'Invalid email submitted - mail not being sent.';
exit;
}
contains_bad_str($email);
contains_bad_str($subject);
contains_bad_str($body);//replace body with $body
contains_newlines($email);
contains_newlines($subject);
$body = $body . " From: " . $name;
$headers='MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
$headers .= "From: $email" . "\r\n";
mail ($to, $subject, $body, $headers);
echo "Thanks for submitting.";