
kaiman
Members-
Posts
104 -
Joined
-
Last visited
Everything posted by kaiman
-
display xml pubDate from mysql database with php
kaiman replied to kaiman's topic in PHP Coding Help
The date is stored in the db like this and the timestamp is set to NOW() from another script that adds the news via a form: `date` datetime NOT NULL default '0000-00-00 00:00:00', -
I am trying to display the publication date a news article from a database in xml via php, but it won't show up. There is no errors it just gets to the text rown and then doesn't display anything else. My question: What is the correct way to display dates from a database in xml via php? Below is my code. All help is appreciated. kaiman <?php header('content-type: text/xml'); // connects to server and selects database. include ("dbconnect.inc.php"); // table name $tbl_name="news"; // select info from database $sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT 0, 10"; $result=mysql_query($sql); while ($row=mysql_fetch_array($result)){ $row_date = strtotime($row['date']); // display xml page echo '<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"> <channel> <title>My Site</title> <description>My Site News Feed</description> <copyright>Copyright '.date('Y').' My Site</copyright> <language>en-us</language> <link>http://www.mysite.com/</link>'; echo '<item>\n <title>'.$row['title'].'</title> <description><![CDATA[<p> '.$row['text'].' </p>]]></description> <link>http://www.mysite.com/news/</link> <pubDate>'.date('F, j, Y \a\t g:i A, T', $row_date).'</pubDate> <managingEditor>'.$row['author'].'</managingEditor> </item>'; } echo '</channel> </rss>'; ?>
-
help displaying first 10 words in database field
kaiman replied to kaiman's topic in PHP Coding Help
SOLVED Thanks for the reply, I actually realized after I posted and re-looked at the PHP substr page that it was based on characters not words, so I just simplified the variable to this and now it works as expected. // cut news to 110 characters and then display $headline = $row['text']; echo "<p>".substr($headline, 0, 110)."... <a href=\"http://www.mysite.com/news/\">Read more</a></p>\n"; -
I have the following script to display a brief news clip/description on the homepage of a website. I am trying to get it to display the first ten words from the 'text' field, but it just seems to ignore it and display the whole news item, no errors or anything... Any ideas what's going on or what I am missing? Thanks, kaiman <?php // connects to server and selects database. include ("dbconnect.inc.php"); // table name $tbl_name="news"; // select info from database $sql="SELECT * FROM $tbl_name ORDER BY id DESC LIMIT 1"; $result=mysql_query($sql); // display news items while ($row=mysql_fetch_array($result)){ echo "<h1>Latest News</h1>\n"; $row_date = strtotime($row['date']); echo "<h3>".date('F, j, Y', $row_date)."</h3>\n"; // cut news paragraph to ten words if (strlen($row['text']) > 10){ $description = substr($row['text'], 0, 10); } else{ $description = $row['text']; } echo "<p>".$row['text']."... <a href=\"http://www.mysite.com/news/\">Read more</a></p>\n"; } ?>
-
I am using the following lines of code to destroy user $_SESSIONS in PHP. However, after testing it in multiple browsers it seems to work fine in every browser but Safari (Win XP, Mac OS 10.5). <?php session_start(); $_SESSION = array(); session_unset(); session_destroy(); ?> Safari users can just use their back button to get back to the members area. So I was just reading about HTTP headers and came across the following examples. What is the best practice in this situation and which code should I use? Also what is the difference? <?php header("expires: Sun, 19 Nov 1978 05:00:00 GMT"); header("last-modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("cache-control: no-store, no-cache, must-revalidate"); header("cache-control: post-check=0, pre-check=0", FALSE); ?> or <?php header("cache-control: no-store, no-cache, must-revalidate"); header("pragma: no-cache"); header("expires: 0"); ?> Thanks for the help kaiman
-
Anybody done anything similar to this? Thanks, kaiman
-
I found this which essentially does what I want simply using PHP:http://www.phpfreaks.com/forums/index.php?topic=157119 and http://stackoverflow.com/questions/66422/how-do-i-rotate-an-image-at-12-midnight-every-day The question is how would I simply use it for the text in the file in my first post?
-
Still looking for answers on this... Thanks, kaiman
-
Thanks mikesta707, I would rather just keep in all in one file rather then quering a database. Also, I was just reading about cron jobs and it seems they can be pretty server intensive so I am reluctant to use that approach... How could I grab the server time and then use PHP operators to tell it to update itself every 24 hours? I am struggling with the logic on how to do this... Thanks!
-
Hi Everyone, I currently have the following script which randomly displays a message every time a user visits/refreshes a webpage. I would like to adjust it to display a new random message every 24 hours. The problem I am having is how to get the time from the server and tell the script to update every 24 hours. Any suggestions are welcome. Thanks in advance, kaiman <?php // this script will randomly display a featured hike on each page visit // randomly displays hikes srand ((double) microtime() * 1000000); $featuredhike = rand(0,count($hike)-1); echo ($hike[$featuredhike]); // echo to page $hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n"; $hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n"; $hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n"; $hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n"; $hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n"; $hike[] = "<h1>Featured Hike</h1>\n<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean leo. Praesent vitae magna posuere augue iaculis scelerisque...</p>\n"; ?>
-
* SOLVED * Thanks!
-
Yep this is a static system so I will change the question... Just out of curiosity, why does 0 return empty? Thanks, kaiman
-
I am currently working on an account signup script and have a form field called "math" that works as a simple Captcha and asks the following question: Anti-Spam Question: 1 - 1 = ? Of course as we know the answer is zero/0 However, in my php script when I run this code it will return as empty when someone enters the number "0" rather then typing "zero", or "Zero", or "ZERO", but returns true if they enter any of the last three options. <?php // validate math function and check for empty fields if ($math != "0" && $math != "zero" && $math != "Zero" && $math != "ZERO") { echo "You Did Not Answer the Math Problem Correctly"; exit ; } if (empty($math)) { echo "Please Complete All Form Fields"; exit ; } ?> My question is how come it won't recognize the number 0 and is there a way to prevent this from happening? Any help would be appreciated. Thanks, kaiman
-
As the title states, I am trying to copy column1 to column2 and then update column1 and column3. This is for a login script where I want the lastlogin timestamp to be replaced by the currentlogin timestamp and then have the currentlogin timestamp and users currentip updated for my records. I am just looking for advice on the method/syntax before running this. Here is what I have so far: // get current ip address <?php $currentip = getenv('REMOTE_ADDR'); // update database by copying the timestamp from currentlogin to lastlogin and inserting the current ip address and current login timestamp $sql="UPDATE $tbl_name SET lastlogin = currentlogin, currentip = '$currentip', currentlogin = NOW() WHERE username='$username' AND password='$pass'"; mysql_query($sql) or trigger_error("A MySQL error has occurred!"); ?> Thanks! kaiman
-
* SOLVED * Thanks to everyone who has helped me sort this out over the past 24 hours... Cheers! kaiman
-
Thanks, I'll give this a try... BTW - I've been thinking the same thing... How about a user level scheme more like this one? 0 = banned user 1 = guest 2 = user - default 3 = author 4 = moderator 5 = admin
-
Yes! Finally got it working, he he! FYI, I changed my session stuff to this to get it to run correctly: if ($_SESSION['level'] == '1') { Thanks mrMarcus and others for all your help! The next question is - How do I do a check to make sure that regular users can't see admin pages, etc. So far I have this basic session check: <?php session_start(); if(!isset($_SESSION['username'])){ header("Location: http://www.example.com/login/" ); exit; } ?> How would I check for levels? Would something like this work? <?php // admin session check session_start(); if(!isset($_SESSION['username'])){ header("Location: http://www.example.com/login/" ); exit; } else ($_SESSION['level'] < 4) { echo "You Don't Have Permission to View an Admin Page"; exit (0); } ?> or do I need to pull the array from the db and register the $_SESSION 'level' again? Thanks a ton! kaiman
-
Still no dice! After trying both level checks it just errors out and won't redirect? It is registering the $_SESSION though, as I can manually enter the URL for the redirect page and it doesn't error out?? Here is what I have right now: <?php // connects to server and selects database. include ("dbconnect.inc.php"); // table name $tbl_name="registered_members"; // removes magic_quotes_gpc slashes function stripQuotes($arg) { if (get_magic_quotes_runtime()) { return stripslashes($arg); } else { return $arg; } } // protect against mysql injection function cleanString($string){ htmlentities(mysql_real_escape_string($string)); return $string; } // username and password sent from login form $username = stripQuotes($_POST['username']); $username = cleanString($username); $pass = sha1($_POST['pass']); // select info from database $sql="SELECT id, level FROM $tbl_name WHERE username='$username' AND password='$pass' LIMIT 1"; // LIMIT 1 will stop mysql from searching once it has found the result $result=mysql_query($sql) or trigger_error("A MySQL Error Has Occured!"); //Perhaps there was an error with the query? // mysql_num_row counts the table row $count = mysql_num_rows($result); // if result matched $username and $pass, table row must be 1 row if($count === 1){ // register $_SESSION session_start(); $_SESSION['username'] = $username; $_SESSION['pass'] = $pass; // pull rows into variables while (list($id, $level) = mysql_fetch_row($result)) { $_SESSION['id'] = $id; $_SESSION['level'] = $level; } } else { echo "Incorrect Username or Password"; exit ; } // var_dump($row['id']); // var_dump($row['level']); // user levels // 0 = guest // 1 = user - default // 2 = auther // 3 = moderator // 4 = admin // 5 = banned user // check user levels === will check that type is also same (ie integer) if ($_SESSION['level'] === 1) { header("http://www.example.com/user/"); exit (0); } elseif ($_SESSION['level'] === 2) { header("Location: http://www.example.com/author/"); exit (0); } elseif ($_SESSION['level'] === 3) { header("Location: http://www.example.com/moderator/"); exit (0); } elseif ($_SESSION['level'] === 4) { header("Location: http://www.example.com/admin/"); exit (0); } else { echo "You Don't Have Permission to View This Page"; } ?> Thanks.
-
Okay, I changed the user level to 4 in the db and then ran the script and it works... SO how do I change this section to run a check and redirect if less then 4? Thanks again, kaiman
-
mrMarcus was correct, I am still getting the error after running Andy-H's script line for line (thanks for the help though Andy). I double checked the password and it is correct. The test user I am using has a level of 1, not 4 - I am trying to run a check to determine the level and redirect... what do you mean that the 'level' in the db is 4? Shouldn't this work if 'level' 1 as well?
-
Haha dreamlove, very funny! I understand your point, but right now I am more concerned with function over form...
-
Yes, it's just the one db entry. Still just coming back with error: You Do Not Have Permission... and doesn't redirect the page via header: Location blah, blah, blah. Here is the whole script as I have it so far: <?php // connects to server and selects database. include ("dbconnect.inc.php"); // table name $tbl_name="registered_members"; // removes magic_quotes_gpc slashes function stripQuotes($arg) { if (get_magic_quotes_runtime()) { return stripslashes($arg); } else { return $arg; } } // protect against mysql injection function cleanString($string){ htmlentities(mysql_real_escape_string($string)); return $string; } // username and password sent from login form $username = stripQuotes($_POST['username']); $username = cleanString($_POST['username']); $pass = sha1($_POST['pass']); // select info from database $sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$pass'"; $result=mysql_query($sql); // mysql_num_row counts the table row $count=mysql_num_rows($result); // pull rows into array $row = mysql_fetch_assoc($result); // print_r($row); // if result matched $username and $pass, table row must be 1 row if($count==1){ // register $_SESSION session_start(); $_SESSION['username'] = $username; $_SESSION['pass'] = $pass; $_SESSION['id'] = $row['id']; $_SESSION['level'] = $row['level']; } else { echo "Incorrect Username or Password"; exit ; } // var_dump($row['id']); // var_dump($row['level']); // user levels // 0 = guest // 1 = user - default // 2 = auther // 3 = moderator // 4 = admin // 5 = banned user // check user levels if ($_SESSION['level'] == '1') { header("http://www.example.com/user/"); } if ($_SESSION['level'] == '2') { header("Location: http://www.example.com/author/"); } if ($_SESSION['level'] == '3') { header("Location: http://www.example.com/moderator/"); } if ($_SESSION['level'] == '4') { header("Location: http://www.example.com/admin/"); } else { echo "You Don't Have Permission to View This Page"; exit ; } ?> Thanks again (this is my first foray into setting up a complete user level system with PHP/MySQL and I am learning alot!) kaiman
-
Here is the partial results of the running a print r. You can clearly see the two columns id and level, but I am still getting the error below: You Don't Have Permission to View This Page. Array ( [id] => 1 [level] => 1 ) BTW my two columns look like this in my db_table: `id` int(4) NOT NULL auto_increment, `level` int(4) NOT NULL default '1', PRIMARY KEY (`id`) Any other ideas? Thanks again! kaiman
-
Sorry that was my sticky finger on that last post. Here is the code I am trying, but it still comes back NULL. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { printf($row['id'], $row['level']); } How would I go about pulling those rows into the mysql_fetch_array?