Blog/Sign in/Email Activation/ = Problems
Posted: Fri Mar 02, 2012 11:22 pm
Hey guys wondering if you can he;lp i have watched your tutorials at least 10 times each and can't find what is stoping my code from working. i am extremely new and of course got myself in a project that i have no idea what I'm doing. i have learned lots and figured stuff out however these amazing life saving tutorials of yours i came a crossed just aren't working for me. so first problem
BLOG!
blog list error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 43
blog read error:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 8
Invalid post ID.
with the pid in the search bar, if i forget that part it just states invalid post!
posts.inc.php:
Fatal error: Cannot redeclare get_posts() (previously declared in /home/content/50/8811650/html/posts.inc.php:37) in /home/content/50/8811650/html/posts.inc.php on line 75
login in:
Username or Password incorrect! even though it is correct it won't log me in it just states that they are incorrect.
email validation:
won't actually activate the user, and it won't delete the activation code from the table???
My Code
Blog_list
Blog_read
blog_posts
index
user.inc.php
init.inc.php
BLOG!
blog list error:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 43
blog read error:
Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/content/50/8811650/html/coreblog/initblog/posts.inc.php on line 8
Invalid post ID.
with the pid in the search bar, if i forget that part it just states invalid post!
posts.inc.php:
Fatal error: Cannot redeclare get_posts() (previously declared in /home/content/50/8811650/html/posts.inc.php:37) in /home/content/50/8811650/html/posts.inc.php on line 75
login in:
Username or Password incorrect! even though it is correct it won't log me in it just states that they are incorrect.
email validation:
won't actually activate the user, and it won't delete the activation code from the table???
My Code
Blog_list
[syntax=php]<?php ini_set('display_errors',1); error_reporting(E_ALL); include ("coreblog/initblog.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php $posts = get_posts(); foreach ($posts as $post){ ?> <h2><a href="blog_read.php?pid=<?php echo $post['id']; ?>"><?php echo $post['title']; ?></a></h2> <h4>By<?php echo $post['user']; ?> on <?php echo $post['date']; ?></h4> <h4>(<?php echo $post['total_comment'];?>comments, last comment<?php echo $post['last_comment']; ?>)</h4> <hr /> <p><?php $post['preview']; ?></p> <?php } ?> </body> </html>[/syntax]
Blog_read
<?php ini_set('display_errors',1); error_reporting(E_ALL); include ("coreblog/initblog.php"); if(isset($_GET['pid'], $_POST['user'], $POST['body'])){ if (add_comment($GET['pid'], $_POST['user'], $_POST['body'])){ header("Location: blog_read.php?pid={$_GET['pid']}"); }else{ header('Location: blog_list.php'); } die(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <?php if (isset($_GET['pid']) === false || valid_pid($_GET['pid']) === false){ echo 'Invalid post ID.'; }else{ $post = get_post($_GET['pid']); ?> <h2><?php echo $post['title']; ?></h2> <h4>By<?php echo $post['user']; ?> on <?php echo $post['date']; ?> (<?php echo count($post['comments']); ?> comments)</h4> <hr /> <p><?php $post['body']; ?></p> <hr /> <?php foreach($post['comments'] as $comment){ ?> <h4>By<?php echo $comment['user']; ?> on <?php echo $comment['date']; ?></h4> <p><?php echo $comment['body']; ?></p> <hr /> <?php } ?> <form action="" method="post"> <p> <label for="user">Name</label> <input type="text" name="user" id="user" /> </p> <p> <textarea name="body" rows="20" collum="60"></textarea> </p> <p> <input type="submit" value="Add Comment" /> </p> </form> <?php } ?> </body> </html>
blog_posts
<?php ini_set('display_errors',1); error_reporting(E_ALL); include ("coreblog/initblog.php"); if (isset($_POST['user'], $_POST['title'], $_POST['body'])){ add_posts($_POST['user'], $_POST['title'], $_POST['body']); header('Location: blog_list.php'); die(); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Untitled Document</title> </head> <body> <form action="" method="post"> <p> <label for="user">Name</label> <input type="text" name="user" id="user" /> </p> <p> <label for="user">Title </label> <input type="text" name="title" id="title" /> </p> <p> <textarea name="body" rows="20" collums="60"></textarea> </p> <p> <input type="submit" value="Add Comment" /> </p> </form> </body> </html>posts.inc.php
<?php //checks if the given post id is in the table function valid_pid ($pid){ $pid = (int)$pid; $total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` {$pid}"); $total = mysql_result($total, 0); ini_set('display_errors',1); error_reporting(E_ALL); if ($total != 1){ return false; }else{ return true; } } //festches a summery of all the blog posts function get_posts(){ $sql = "SELECT `posts`.`post_id` AS `id`, `posts`.`post_title` AS `title`, LEFT(`posts`.`post_body`, 512) AS `preview`, `posts`.`post_user` AS `user`, DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`, `comments`.`total_comments`, DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment` FROM `posts` LEFT JOIN ( SELECT `post_id`, COUNT(`comment_id`) AS `title_comments`, MAX(`comment_date`) AS `last_comment` FROM `comments` GROUP BY `post_id` ) AS `comments` ON `posts`.`post_id` = `comments`.`post_id` ORDER BY `posts`.`post_date` DESC"; $posts = mysql_query($sql); $rows = array(); while (($row = mysql_fetch_assoc($posts)) !== false){ $rows[] = array( 'id' => $row['id'], 'title' => $row['title'], 'preview' => $row['preview'], 'user' => $row['user'], 'date' => $row['date'], 'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'], 'last_comment' => ($row['last_comment'] === null) ? 'never' : $row['last_comment'] ); } return $rows; } ini_set('display_errors',1); error_reporting(E_ALL); //fetches a single post from the table function get_post($pid){ $pid = (int)$pid; $sql = "SELECT `post_title` AS `title`, `post_body` AS `body`, `post_user` AS `user`, `post_date` AS `date` FROM `posts` WHERE `post_id` = {$pid}"; $post = mysql_query($sql); $post = mysql_fetch_assoc($post); $post['comments'] = get_comments($pid); return $post; } //adds new blog entry function add_post($name, $title, $body){ $name = mysql_real_escape_string(htmlentities($name)); $title = mysql_real_escape_string(htmlentities($title)); $body = mysql_real_escape_string(nl2br(htmlentities($body))); mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES ('{$name}', '{$title}', '{$body}', NOW())"); } ?>
index
<?php ini_set('display_errors',1); error_reporting(E_ALL); include('core/init.inc.php'); $errors = array(); if (isset($_POST['username'], $_POST['password'])){ if (empty ($_POST['username'])){ $errors[] = 'The username cannot be empty!'; } if (empty ($_POST['password'])){ $errors[] = 'The password cannot be empty!'; } if (valid_credentials($_POST['username'], sha1($_POST['password'])) === false){ $errors[] = 'Username or Password incorrect!'; } if (empty($errors) && is_active($_POST['username']) === false){ $errors[] = 'This account has not yet been activated!'; } if(empty($errors)){ if (isset($_POST['set_cookie']) && $_POST['set_cookie'] == '1'){ setcookie('username', $_POST['username'], time() + 604800); setcookie('password', sha1($_POST['password']), time() + 604800); } $_SESSION['username'] = htmlentities($_POST['username']); header('Location: protected.php'); die(); } } ?> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Login - Know Quest</title> <style type="text/css"> <!-- body { font: 100%/1.4 Verdana, Arial, Helvetica, sans-serif; background: #42413C; margin: 0; padding: 0; color: #000; background-color: #FFFFFF; } /* ~~ Element/tag selectors ~~ */ ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */ padding: 0; margin: 0; } h1, h2, h3, h4, h5, h6, p { margin-top: 0; /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */ padding-right: 15px; padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */ text-align: left; } a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */ border: none; } /* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */ a:link { color: #42413C; text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */ } a:visited { color: #6E6C64; text-decoration: underline; } a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */ text-decoration: none; } /* ~~ this fixed width container surrounds the other divs ~~ */ .container { width: 960px; background: #FFF; margin: 0 auto; /* the auto value on the sides, coupled with the width, centers the layout */ } /* ~~ the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo ~~ */ .header { background-color: #F00; } /* ~~ This is the layout information. ~~ 1) Padding is only placed on the top and/or bottom of the div. The elements within this div have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design. */ .content { padding: 10px 0; } /* ~~ The footer ~~ */ .footer { padding: 10px 0; background-color: #ED1D23; color: #FFF; text-align: right; } /* ~~ miscellaneous float/clear classes ~~ */ .fltrt { /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */ float: right; margin-left: 8px; } .fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */ float: left; margin-right: 8px; } .clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */ clear:both; height:0; font-size: 1px; line-height: 0px; } .container .header { text-align: center; } .container .content table { text-align: left; } .container .footer h5 { text-align: left; } .container .content { text-align: left; } .tt { text-align: center; } .container .footer { text-align: right; } .container .footer h6 { text-align: right; } .container .content form label { text-align: left; } .container .content h5 { text-align: center; } .container .content { text-align: center; } --> </style></head> <body> <div class="container"> <div class="header"><!-- end .header --> <p> </p> <p><img src="Images/Logo.gif" alt="Logo" width="807" height="207" align="left" /></p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <p> </p> </div> <div class="content"> <div> <?php if (empty($errors) === false){ ?> <ul> <?php foreach ($errors as $error){ echo "<li>{$error}</li>"; } ?> </ul> <?php }else{ echo 'Need an account? <a href="sign up1.php">Sign Up </a>'; } ?> </div> </p> </h5> <p> </p> <form action="" method="post" name="login"> <table width="100%" border="0"> <tr> <td width="14%">Username:</td> <td width="86%"><input type="username" name="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['$username']); ?>"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password"></td> </tr> <tr> <td><label for="set_cookie" >Remember me:</label></td> <td><input type="checkbox" name="set_cookie" id="set_cookie" value="1"/></td> </tr> </table> <p><br> <input type="submit" name="submit" value="Login"><br> </p> <p> </p> <p> </p> </form> </div> <div class="footer"> <!-- end .footer --> <h6> </h6> <form> Search <input type="text" size="30" onKeyUp="showResult(this.value)" /> <h5>_______________________________________________________________________________________________________</h5> <h5> <!-- end .footer -->KnowQuest © 2012 English (Canada) | Terms of use | Search | Prof. Finder | Ratings | Your Research </h5> </form> </div> <!-- end .container --></div> </body> </html>
user.inc.php
<?php ini_set('display_errors',1); error_reporting(E_ALL); //fetches all users from the user table function fetch_users(){ $result = mysql_query("SELECT `user_id` AS `id`, `user_username` AS `username` FROM `users`"); $users = array(); while (($row = mysql_fetch_assoc($result)) !== false){ $user[] = $row; } return $users; } //fetches user profile information function fetch_user_info($uid){ $uid = (int)$uid; $sql = "SELECT `user_id` AS `id`, `user_username` AS `username`, `user_firstname` AS `firstname`, `user_lastname` AS `lastname`, `user_institution` AS `institution`, `user_about institution` AS `aboutinstitution`, `user_professional title` AS `professionaltitle`, `user_professional research` AS `professionalresearch`, `user_prefessional website` AS `professionalwebsite`, `user_personal website` AS `personalwebsite`, `user_personal email` AS `personalemail`, `user_professional email` AS `professionalemail`, `user_about your research` AS `aboutyourresearch`, `user_about yourself` AS `aboutyourself`, `user_social media` AS `socialmedia`, FROM `users` WHERE `user_id` = {$uid}"; $result = mysql_query($sql); $info = mysql_fetch_assoc($result); $info['pictureid'] = (file_exists("{GLOBALS['$path']}/user_portfolio picture{$info['id']}.jpg")) ? "core/user_portfolio picture{$info['id']}.jpg" : "core/user_portfolio picture/default.jpg"; return $info; } //updates the user infos profile info function set_profile_info($professonalwebsite, $personalwebsite, $institution, $aboutinstitution, $professionalresearch, $professionaltitle, $professionalemail, $aboutyourresearch, $aboutyourself, $socialmedia, $personalemail, $pictureid){ $professinalemail = mysql_real_escape_string(htmlentities($professionalemail)); $personalemail = mysql_real_escape_string(htmlentities($personalemail)); $personalwebsite = mysql_real_escape_string(htmlentities($personalwebsite)); $professonalwebsite = mysql_real_escape_string(htmlentities($professonalwebsite)); $institution = mysql_real_escape_string(htmlentities($institution)); $aboutinstitution = mysql_real_escape_string(htmlentities(nl2br($aboutinstitution))); $professionalresearch = mysql_real_escape_string(htmlentities($professionalresearch)); $professionaltitle = mysql_real_escape_string(htmlentities($professionaltitle)); $aboutyourresearch = mysql_real_escape_string(htmlentities(nl2br($aboutyourresearch))); $aboutyourself = mysql_real_escape_string(htmlentities(nl2br($aboutyourself))); $socialmedia = mysql_real_escape_string(htmlentities($socialmedia)); if (file_exists($pictureid)){ $src_size = getimagesize($pictureid); if ($src_size['mime'] === 'image/jpeg'){ $src_img = imagecreatefromjpeg($pictureid); }else if ($src_size['mime'] === 'image/png'){ $src_img = imagecreatefrompng($pictureid); }else if ($src_size['mime'] === 'image/gif'){ $src_img = imagecreatefromgif($pictureid); }else{ $src_img = false; } if ($src_img !== false){ $thumb_width = 200; if ($src_size[0] <= $thumb_width){ $thumb = $src_img; }else{ $new_size[0] = $thumb_width; $new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width; $thumb = imagecreatetruecolor($new_size[0], $new_size[1]); imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]); } imagejpeg($thumb, "{GLOBALS[$path]}/user_portfolio picture/{$SESSION['uid']}.jpg"); } } $sql = "UPDATE `users` SET `user_personal website` = '{$presonalwebsite}' `user_institution` = '{$institution}' `user_about institution` = '{$aboutinstitution}' `user_professional research` = '{$professionalresearch}' `user_professional title` = '{$professionaltitle}' `user_about your research` = '{$aboutyourresearch}' `user_about yourself` = '{$aboutyourself}' `user_personal email` = '{$personalemail}' `user_professional website` = '{$professionalwebsite}' `user_professional email` = '{$professionalemail}' `user_social media` = '{$socialmedia}' WHERE `user_id` = {$_SESSION['uid']}"; mysql_query($sql); } //check if given username exsists in the database function user_exists($user){ $user = mysql_real_escape_string($user); $total = mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}'"); return (mysql_result($total, 0) == '1') ? true : false; } //check if the given username and password combinations are valid function valid_credentials($user, $pass){ $user = mysql_real_escape_string($user); $pass = mysql_real_escape_string($pass); $total= mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_username` = '{$user}' AND `user_password` = '{$pass}'"); return (mysql_result($total, 0) == '1') ? true : false; } // checks to see is user account is active function is_active($user){ $user = mysql_real_escape_string($user); $sql = "SELECT COUNT (`activations`.`user_id`) FROM `users` INNER JOIN `activations`.`user_id` ON `users`.`user_id` = `activations`.`user_id` WHERE `users`.`user_username` = '{$user}'"; $result = mysql_query($sql); return (mysql_result($result, 0) == '0') ? true : false; } //acctivates the account related to the given activation code function activate_account($aid){ $aid = mysql_real_escape_string($aid); mysql_query("DELETE FROM `activations` WHERE `activation_code` = '{$aid}'"); } //adds a user to the database function add_user($user, $email, $pass, $first, $last){ $user = mysql_real_escape_string(htmlentities($user)); $email = mysql_real_escape_string($email); $pass = sha1($pass); $firstname = mysql_real_escape_string(htmlentities($firstname)); $lastname = mysql_real_escape_string(htmlentities($lastname)); $charset = array_flip(array_merge(range('a', 'z'), range('A', 'Z'), range('0', '9'))); $aid =implode('', array_rand($charset, 10)); $body = <<<EMAIL Thank you for signing up with knowquest. To activate your account, please click the link below http://www.jasonmassieportfolio.com/activate.php?{$aid} EMAIL; mail($email, 'Your new account at Knowquest.com', $body, 'From: admin@knowquest.com'); mysql_query("INSERT INTO `users` (`user_username`, `user_email`, `user_password`) VALUES ('{$user}', '{$email}', '{$pass}')"); $user_id = mysql_insert_id(); mysql_query("INSERT INTO `activations` (`user_id`, `activation_code`) VALUES ({$user_id}, '{$aid}')"); } ?>
init.inc.php
<?php session_start (); $exceptions = array ('sign up1', 'index', 'activate', 'member1', 'Edit', 'user_lists', 'blog_posts'); $page = substr(end(explode('/', $_SERVER['SCRIPT_NAME'])), 0, -4); mysql_connect("phplogin113.db.8811650.hostedresource.com","phplogin113","Hookups1"); mysql_select_db("phplogin113"); $path = dirname(__FILE__); include ("{$path}/init/user.inc.php"); $_SESSION['uid'] = 1; if (isset($_COOKIE['username'], $_COOKIE['passwords']) && isset($_SESSION['username']) === false){ if (valid_credentials($_COOKIE['username'], $_COOKIE['password'])){ $_SESSION['username'] = htmlentities($_COOKIE['username']); setcookie('username', $_COOKIE['username'], time() + 604800); setcookie('password', $_COOKIE['password'], time() + 604800); } } if (in_array($page, $exceptions) === false){ if (isset($_SESSION['username']) === false){ header('Location: index.php'); die(); } } ?>