1,076,263 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by cjohnweb which have been Voted Up

cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0

If you are interested, I use sessions to hold user messages. I've create just a small handful of functions, and some css so it can be placed anywhere on any website and just work :)

Here we go:

function set_user_message($msg, $class = "warning"){
$_SESSION['user']['msg'][$class][] = $msg;
}

function read_user_message(){
foreach ($_SESSION['user']['msg'] as $k => $v){
foreach ($v as $kk => $vv){
echo "<div class=\"$k\">$vv</div>\n";
}
}
unset($_SESSION['user']['msg']);
}

function check_user_message(){
if(count($_SESSION['user']['msg']) > 0){
return(true);
}else{
return(false);
}
}

/*
Redirect user to new location, with status codes!
  MUST BE USED IN PHP BEFORE ANY TEXT IS CALLED TO 
  THE SCREEN, SENDS HEADER INFORMATION
302 = Temporary redirect (default)
301 = Permanent redirect
*/
function redirect($location,$status=302){
$str = explode("?",$location);
parse_str($str[1], $output);
foreach ($output as $k => $v){$uri .= "$k=".urlencode($v)."&";}
$uri = substr($uri,0,-1);
$location = $str[0];
if(!empty($uri)){$location .= "?".$uri;} 
@header("Location: $location", true, $status);
//header("Refresh: 0;url=$location");
}


function check_error_user_message(){
if(count($_SESSION['user']['msg']['error']) > 0){
return(true);
}else{
return(false);
}
}

/* Used on one site to make it align properly - display the user messages */
function user_messages(){
if(check_user_message() == true){
echo "<div style=\"float:left; width:100%; margin:10px; padding:10px; border:0px solid #000000;\">";
read_user_message();
echo "</div>";
echo "<br class=\"clear\" />";
}
}



.info, .success, .warning, .error, .validation, .search, .tips {
width:400px;
border: 1px solid;
margin: 0px auto;
margin-top:10px;
margin-bottom:10px;
padding:10px;
padding-left:50px;
background-repeat: no-repeat;
background-position: 10px center;
list-style:none;
}

.info ul, .success ul, .warning ul, .error ul, .validation ul, .search ul, .tips ul {
list-style:none;
}

.info a, .success a, .warning a, .error a, .validation a, .search a, .tips a {
text-decoration:none;
}

.tips {
border: 0px solid;
margin: 10px 0px;
padding:15px 10px 15px 50px;
background-repeat: no-repeat;
background-position: 10px center;
/*color: #00529B;
background-color: #BDE5F8;*/
font-size:16px;
color: #000000;
/*background-color: #FFD685;*/
background-image: url(images/tips.png);
}

.tips a,.tips a:visited,.tips a:active,.tips a:link,.tips a:hover{
font-weight:bold;
font-style:italic;
text-decoration:underline !important;
}

.search {
color: #00529B;
background-color: #BDE5F8;
background-image: url(images/search.png);
}

.info {
color: #00529B;
background-color: #BDE5F8;
background-image: (images/info.png);
}

.success {
color: #4F8A10;
background-color: #DFF2BF;
background-image:url(images/success.png);
}

.warning {
color: #9F6000;
background-color: #FEEFB3;
background-image: url(images/warning.png);
}

.error {
color: #D8000C;
background-color: #FFBABA;
background-image: url(images/error.png);
}

.validation {
color: #D63301;
background-color: #FFCCBA;
background-image: url(images/validation.png);
}

Save the css file in the root directiory, I name it user-messages.css, and link to it in your index, template or every page that will use the script.

Include the functions in your PHP where each page that displays messages can access it, and also so the pages that process your code can access it.

Here is example of how to use it:

<?php

// processing a login (short hand to show usage)

if(empty($_POST['email'])){set_user_message("An email is required to log in","error");}
if(empty($_POST['pass'])){set_user_message("A password is required to log in","error");}

// other error checking here

if(check_error_user_message() == true){
redirect("index.php?page=login"); // send them back to login page
}else{
//login the user here

set_user_message("Logged in successfully","success");

redirect("index.php?page=account"); //redirect them to their account page or w/e
}


-----------------

index.php

<DOCTYPE html>
<html>
<head>
</head>
<body>

<h1>Login</h1>

<?php user_messages(); ?>

<form>
<input name="email" />
...
</form>

</body>
</html>
?>

It's really easy to use, just set user messages, and read user messages. If you noticed, I have the check_error_user_message(); function. It tells me if an error has been set. Basically I can use the error mesage as an indicator that we should not even attempt to login the user because, say, they didn't enter a password. You can use it how ever you see fit though.

I started this whole thing because the redirect function has to be called before any text is placed on the screen. So I was trying to send messages in the url as $_GET info, and that was a mess. This is much much cleaner, the functions save everything in sessions so when you set a message you don't have have to worry about it any more, everything just works :)

Don't forget to start your scripts with session_start(); or sessions will not work.

Questions, comments, feedback and improvements are always welcome. Please let me know some things if you try it - ease of installing to your site, simplicity in understainding, remembering the, and of course any bugs.

Thanks

cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0

I would love to share with you a nice function I made just for this. But to impliment it you might need to give me some more info - basically just your database schema - table name and column names. The rest is all up to you!

For your project you want to do something like this

general idea of your database schema
ip,date,country,city,region

$paginate = pagination("SELECT * FROM visitors ORDER BY date DESC","main.php?pagination=",$_GET['pagination'],10,true);

The function should handle all of your pagination math and what not, now you just need to use it's output....

<?php

$gv = mysql_query($paginate['sql']);
$cgv = mysql_num_rows($gv);
if($cgv > 0){
while($agv = mysql_fetch_array($gv)){
/*mysql columns ip,date,country,city,region*/

echo "IP: $ip<br />";
echo "Country: $country<br />";
echo "City: $city<br />";
echo "Region: $region<br />";
echo "Date: $date<br />";

}

echo $return['total']." pages of results.<br /><br />";
echo $return['display']."<br /><br />";
echo $return['pages'];

}else{
echo "no results";
}
?>

You should be able to intergrate it into your own project pretty easily. Let me know if you need help - email me john@iluvjohn.com

Don't forget to put the function into your script too:

 <?php                                               
 /*******************************************************************************
 *
 *   Pagination Functions
 *   By John Minton 10/16/2010
 *   Originally for  GardenOfTomorrow.Com 
 *
 *
 *
 *   string pagination( string $sql, string $url, [int $pagination, int $perpage, string $usehtml ] )
 *
 *   string $sql
 *     This can be nearly any SQL "SELECT" statment, such as, "SELECT * 
 *     FROM posts", that can end with a MySQL "LIMIT #,#" statement. The 
 *     pagination function will turn this SQL statement into the proper 
 *     "SELECT * FROM posts LIMIT #1 , #2" used for pagination. 
 *             
 *   string $url
 *     This is the url used in the pagination links, minus the actual 
 *     pagination page #. ie
 *       http://domain.com/somepage.php?pagination=
 *       ?page=home&pagination=
 *       or just "?pagination=" for simple projects!
 *     
 *   int $pagination
 *     This variable carries the page that you are on in your pagination 
 *     script. It will default to 1 if its not numeric, or greater than the 
 *     total # of pages. eg set to $_GET['pagination']
*
 *  int $perpage
 *     This variable specifies how many results you want per page. Defaults 
 *     to 10 if not numeric or left blank.
 *     
 *  string $usehtml
 *     This variable can be set to true or false. If set to false, it will 
 *     return text only. If set to true, or left blank, it will return HTML 
 *     divs around the returned results. Defaults to true.     
 *
 *     
 *  Usage:
 *  
 *  $return = pagination("SELECT * FROM community ORDER BY c_date 
 *   DESC","somepage.php?pagination=",$pagination,10,true);
 *    
 *  echo $return['pages'];
 *  echo $return['display'];
 *   
 *  $gl = mysql_query($return['sql']);
 *   
 *  // Will Return an array of:
 *  $return['sql'] Your original SQL statement, now with proper pagination "LIMIT #,# " appended!
 *  $return['total'] Total number of pages in a <div id='pagination-total'> 
 *  $return['pages'] You pagination links in a <div id='pagination-pages'> 
 *  $return['display'] You pagination links in a <div id='pagination-display'> 
 *       
 *  $paginate = pagination("SELECT * FROM simple_community ORDER BY sc_date DESC","somepage.php?pagination=",$pagination,10,true);
 *  print_r($paginate);
 * 
 *     Array {
 *     [sql] => SELECT * FROM simple_community ORDER BY sc_date DESC LIMIT 0, 10
 *     [total] => <div id="pagination-total">97 Results</div>
 *     [pages] => <div id="pagination-pages">Pages: <a href="?page=community&section=trade&pagination=1">1</a> <a href="?page=community&section=trade&pagination=2">2</a> <a href="?page=community&section=trade&pagination=3">3</a> <a href="?page=community&section=trade&pagination=4">4</a> <a href="?page=community&section=trade&pagination=5">5</a> <a href="?page=community&section=trade&pagination=6">6</a> <a href="?page=community&section=trade&pagination=7">7</a> <a href="?page=community&section=trade&pagination=8">8</a> <a href="?page=community&section=trade&pagination=9">9</a> <b>10</b> </div>
 *     [display] => <div id="pagination-display">Displaying results 91 - 97</div>
 *     }
 *     
 *     
 ******************************************************************************/
function pagination($sql,$pagesurl,$pagination,$perpage = null,$usehtml = null){

if($perpage == null || !is_numeric($perpage)){$perpage = "10";}
if($usehtml == null || $usehtml == true){$usehtml = true;}else{$usehtml = false;}
$gt = mysql_query($sql);
$total = mysql_num_rows($gt);
$pgs = ceil($total/$perpage);
if($pgs < 1){$pgs = 1;}
if(!is_numeric($pagination) || $pagination > $pgs){$pagination = "1";}
for($i = 1;$i<=$pgs;$i++){
if($i == $pagination){
$pages .= "<b>$i</b> ";
}else{
$pages .= "<a href=\"$pagesurl"."$i\">$i</a> ";
}
}
if($usehtml == true){
$pages = "<div id=\"pagination-pages\">Pages: $pages</div>";
}else{
$pages = "Pages: $pages";
}
$nextlimit = $pagination * $perpage - $perpage;
if($usehtml == true){$display = "<div id=\"pagination-display\">";}
$display .= "Displaying results ";
if($pgs == 1){
$display .= $nextlimit; // +1 to offset 0 count
}else{
$display .= $nextlimit+1; // +1 to offset 0 count
}
$display .= " - ";
if($pgs == $pagination){
$display .= $total;
}else{
$display .= $nextlimit+$perpage;
}
$display .= " of $total";
if($usehtml == true){$display .= "</div>";}
if($usehtml == true){$totalresults = "<div id=\"pagination-total\">";}
$totalresults .= "$total Result";
if($total != 1){$totalresults .= "s";}
if($usehtml == true){$totalresults .= "</div>";}
$return['sql'] = $sql." LIMIT $nextlimit, $perpage";
$return['total'] = $totalresults; // 97 Results
$return['pages'] = $pages; // Pages: 1 2 3 <b>4</b> 5, etc
$return['display'] = $display; // Displaying results 31 - 40
return($return);

}
?>
cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0

Hey, This might not be the 100% correct way to do this, but I don't use mysql to create dates. I use PHP's built in date functions. Which ever columns are going to hold a date, set to varchar (20) or so.

You should check http://php.net/manual/en/function.strtotime.php, check out what time formats are valid. Let's say you have a calendar that returns "mm/dd/yyyy h:m:s", you can pass that to strtotime(); and get a timestamp back.

See code example:

<?php
include("config.php"); // connect to db

$current_date = date("U"); // timestamp for right now

$date_from_form = $_GET['date']; // get date from the url, or a form, etc
$past_date = strtotime($date_from_form); // convert the time from the form to a timestamp


//insert to database
$ins = mysql_query("INSERT INTO table (id,lastmeal) VALUES (id,'$past_date')");
if($ins){
echo "Added to database!";
}else{
echo "There was an error: ".mysql_error();
}

echo "<br /><hr /><br />";


// Now lets pull out all the results and convert back to a readable time format:

$gd = mysql_query("SELECT * FROM table");
$cgd = mysql_num_rows($gd);
if($cgd > 0){
while($agd = mysql_fetch_array($gd)){
extract($agd);// extract will set $id and $lastmeal

echo "ID: $id, LastMeal: ".date("r",$lastmeal)."<br />";

}
}else{
echo "There are no results!";
}


?>

Hope that helps

cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0

Yeah...I was just going to say that:

<?php

$var = $_GET['var'];
$var2 = $_POST['var2'];

//should be

$var = mysql_real_escape_string(trim($_GET['var']));
$var2 = mysql_real_escape_string(trim($_POST['var2']));

?>

I trim and escape pretty much everything that comes through POST, GET, FILES, etc.

cjohnweb
Junior Poster
102 posts since Apr 2010
Reputation Points: 26
Solved Threads: 8
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page rendered in 0.0542 seconds using 2.52MB