Scroll down to my most-recent post, and I'm sure you'll find me there asking some newbie question that seems glaringly obvious to everybody else.
Comments
danielneriWP V.I.P.VPS - Virtual Prince of the Server
umm silly question but are you sure you included the database connectivity info at the top? because i was once getting that error becuase i hadnt included the varible info for the database :8
Edit: Okay, started totally over with some new stuff since what I was working with before was giving me so much trouble.
I have one, really BIG question that I've looked everywhere and been unable to find an answer for.
I would like to know how you call a particular bit of information (as a variable) in order to make it display. To explain what I'm getting at, I'm talking about making a page after a user successfully logs into my site, where it greets them with, "Welcome, $username." or something along those lines. Also, similarly, for displaying their profile information later on down the road. However, no matter how much I've tried and butchered PHP, I can't figure out exactly how I do this.
I'm obviously using cookies/sessions and would like to know how I can keep the logged-in user's data available, for it to validate that user's id via the cookie or session and pull the requested data (username, etc) from my database's table.
I have an idea of how to fetch and display data from my database's tables, but I don't want it to be static, I want it to display only the username (or whatever $variable is stuck in there) to whom it belongs, whoever is logged in at the moment, and so I need it to... communicate with the cookie/session somehow. Or however this is done, let me know if I'm totally misinformed.
If anybody could point me in the right direction as to how to possibly create what I'm thinking of (and I know it's possible), I'd greatly appreciate it.
NuvoForum LeaderVPS - Virtual Prince of the Server
Erm, assuming that you have a working member script, you could do something like:
[php]
if(!isset($_COOKIE["myusercookie"])){
echo "My login form goes here";
}else{
$query = "SELECT * FROM users WHERE `userident` = \"".$_COOKIE["myusercookie"]."\"";
$q = mysql_query($query) or die(mysql_error());
while($result = mysql_fetch_array($q)){
echo "Welcome back, ".$result["username"]."<br />My user options go here";
}
}
[/php]
Which should track the cookie saved on the users computer and load the welcome message if the user is logged in (note: no error checking in the code, which you should have).
I suck at PHP, so someone else may give a better explination, but the basic idea is to check the value of the cookie against your database and act on the result.
PHP, CSS, XHTML, Delphi, Ruby on Rails & more. Current project: CMS Object. Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in. Release date: NEVER!!!
Well, thanks for at least replying.
Hmm. Okay, can anybody tell me the basic differences between cookies and sessions? I've heard sessions are better, more secure/reliable, but they also seem to cause slowness as far as page loading if you run a lot of data through it, whereas cookies seem quick and easy, but not secure at all.
I'm obviously interested in this for more of a personal site, not anything business or transaction-related that would need huge security. I just didn't want unhappy hackers messing with my stuff. So can anyone give me any recommendations as to what I should be looking at? Stick to cookies? I'm not even sure how to begin on that... My problem being that I learn by looking at examples and figuring out what makes it tick.
But to the issue: While I already happen to have a decent login system, I want to be able to pass data back and forth so I either need cookies or sessions and I'm not sure which is better, or how to do either. Here's what I've got so far. I have register.html which has my form with the input names username and password, referring to register.php. Not much else you need to know about that... Here's register.php, it has session stuff in it currently, but I can't figure out how to pass the data from this page to the next, and how to keep passing data on to other extended pages where I want info to be displayed.
[PHP]<?
//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();
//require the config file
require ("config.php"); // This sets all my database info
require ("functions.php"); // This sets some variable info
<body onload="FP_popUpMsg('Your password must be between <? echo $min_pass; ?> & <? echo $max_pass; ?> characters.')">
</body>
</html>
<?
exit;
}
//make the connection to the database
$connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());
//make query to database
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
//get the number of rows in the result set
$num = mysql_num_rows($result);
//checks it see if that username already exists
if ($num != 0){
echo "<P>Sorry, that username already exists.</P>";
echo "<P><a href=\"#\" onClick=\"history.go(-1)\">Try another Username.</a></p>";
exit;
//checks to see if the user needs to verify their email address before accessing the site
if ($verify == "0")
{
$mailheaders = "From: www$domain\n";
$mailheaders .= "Your account has been created.\n";
$mailheaders .= "Please activate your account now by visiting this page:\n";
$mailheaders .= "$base_dir/activate.html\n";
$to = "$_POST[/email][email]";
$subject = "Please activate your account";
mail($to, $subject, $mailheaders, "From: No Reply <$adminemail>\n");
}else{
header('Location:login.html');
}
?>[/PHP]
And after registration it sends you back to the login page. Ideally, after the newly registered user logs in, I wanted the "Welcome, $username" and other customized stuff, but that means passing session/cookie info through the register and login pages to get to the successfully-logged-in welcome page.
As usual, any help would be appreciated. I know I'm wading in a bit too deep here, but I'm a quick learner... I just can't find any tutorials on the subject matter, and all the dictionary-style spam on certain PHP sites doesn't help me understand how it actually works.[/email]
NuvoForum LeaderVPS - Virtual Prince of the Server
Sessions are destroyed when the user closes their browser or turns the PC off, nothing is saved to the PC.
This is why sessions are commonly used on sites which are high security as it makes it harder to turn off and stay logged in, which is bad if you aren't using your own PC or if others can use it.
Cookies are saved on the users computer, meaning that even when the computer is restarted, they are still set.
The only way to destroy cookies is to delete them in your browser settings or have the website do it with some kind of logout script.
PHP, CSS, XHTML, Delphi, Ruby on Rails & more. Current project: CMS Object. Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in. Release date: NEVER!!!
Alright, understand that... But..
Can anybody tell me how to:
- Make a cookie that records the userid of the username that just successfully logged in, whether this action would be on the actual login.php or created on the page after that - How to call info from that cookie (for instance, if your userid was 2, it would get userid 2 then query the database for all the info on userid 2, then if I did my "Welcome $username" it would have the necessary data and be able to display the username of userid 2. Am I getting this right? I think all of this is possible, I just can't find tutorials anywhere or anything explaining how to code this.)
I'll be trying the script you posted above, Nuvo, and try to learn cookieness from that, I believe I do have a working member script which can work together with it, but... I'm not certain about anything since what looks like it should work to me isn't always the case.
danielneriWP V.I.P.VPS - Virtual Prince of the Server
okay i know how to make a cookie and pull information from it, but it would have to be done in javascript:
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1)
end = dc.length;
return unescape(dc.substring(begin + prefix.length, end));
}
Comments
[PHP]
$server = "*****";
$dbuser = "*****";
$dbpass = "*****";
$dbname = "*****";
mysql_connect($server,$dbuser,$dbpass) or die ("Could not establish connection");
mysql_select_db($dbname);
// Get users information
$u = $_GET["u"];
$result = mysql_query("SELECT * FROM `fusers` WHERE `userid` = $u LIMIT 1");
while($r=mysql_fetch_array($result)){
// Turn breaks into new lines for biography field
$r[biography] = nl2br($r[biography]);
// Output users information
echo "<small>Your Bio: $r[biography]</small>";[/PHP]
Photoshop Tutorials- Coming soon
Premium PHP Scripts- Coming soon
Haha i should really do some work so i can remove all the coming soon's
I have one, really BIG question that I've looked everywhere and been unable to find an answer for.
I would like to know how you call a particular bit of information (as a variable) in order to make it display. To explain what I'm getting at, I'm talking about making a page after a user successfully logs into my site, where it greets them with, "Welcome, $username." or something along those lines. Also, similarly, for displaying their profile information later on down the road. However, no matter how much I've tried and butchered PHP, I can't figure out exactly how I do this.
I'm obviously using cookies/sessions and would like to know how I can keep the logged-in user's data available, for it to validate that user's id via the cookie or session and pull the requested data (username, etc) from my database's table.
I have an idea of how to fetch and display data from my database's tables, but I don't want it to be static, I want it to display only the username (or whatever $variable is stuck in there) to whom it belongs, whoever is logged in at the moment, and so I need it to... communicate with the cookie/session somehow. Or however this is done, let me know if I'm totally misinformed.
If anybody could point me in the right direction as to how to possibly create what I'm thinking of (and I know it's possible), I'd greatly appreciate it.
[php]
if(!isset($_COOKIE["myusercookie"])){
echo "My login form goes here";
}else{
$query = "SELECT * FROM users WHERE `userident` = \"".$_COOKIE["myusercookie"]."\"";
$q = mysql_query($query) or die(mysql_error());
while($result = mysql_fetch_array($q)){
echo "Welcome back, ".$result["username"]."<br />My user options go here";
}
}
[/php]
Which should track the cookie saved on the users computer and load the welcome message if the user is logged in (note: no error checking in the code, which you should have).
I suck at PHP, so someone else may give a better explination, but the basic idea is to check the value of the cookie against your database and act on the result.
Current project: CMS Object.
Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
Release date: NEVER!!!
Hmm. Okay, can anybody tell me the basic differences between cookies and sessions? I've heard sessions are better, more secure/reliable, but they also seem to cause slowness as far as page loading if you run a lot of data through it, whereas cookies seem quick and easy, but not secure at all.
I'm obviously interested in this for more of a personal site, not anything business or transaction-related that would need huge security. I just didn't want unhappy hackers messing with my stuff. So can anyone give me any recommendations as to what I should be looking at? Stick to cookies? I'm not even sure how to begin on that... My problem being that I learn by looking at examples and figuring out what makes it tick.
But to the issue: While I already happen to have a decent login system, I want to be able to pass data back and forth so I either need cookies or sessions and I'm not sure which is better, or how to do either. Here's what I've got so far. I have register.html which has my form with the input names username and password, referring to register.php. Not much else you need to know about that... Here's register.php, it has session stuff in it currently, but I can't figure out how to pass the data from this page to the next, and how to keep passing data on to other extended pages where I want info to be displayed.
[PHP]<?
//prevents caching
header("Expires: Sat, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: post-check=0, pre-check=0",false);
session_cache_limiter();
session_start();
//require the config file
require ("config.php"); // This sets all my database info
require ("functions.php"); // This sets some variable info
//checks password length
if (password_check($min_pass, $max_pass, $_POST[password]) == "no")
{
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<meta http-equiv="refresh" content="0; url=javascript:history.go(-1)">
<title>Registration</title>
<script language="JavaScript">
<!--
function FP_popUpMsg(msg) {//v1.0
alert(msg);
}
// -->
</script>
</head>
<body onload="FP_popUpMsg('Your password must be between <? echo $min_pass; ?> & <? echo $max_pass; ?> characters.')">
</body>
</html>
<?
exit;
}
//make the connection to the database
$connection = @mysql_connect($server, $dbusername, $dbpassword) or die(mysql_error());
$db = @mysql_select_db($db_name,$connection)or die(mysql_error());
//make query to database
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
//get the number of rows in the result set
$num = mysql_num_rows($result);
//checks it see if that username already exists
if ($num != 0){
echo "<P>Sorry, that username already exists.</P>";
echo "<P><a href=\"#\" onClick=\"history.go(-1)\">Try another Username.</a></p>";
exit;
}else{
$sql = "INSERT INTO $table_name VALUES
('$_POST[realname]', '$_POST[aim]', '$_POST[username]', password('$_POST[password]'), 'Users', '', '', '$pchange',
'$_POST[email]', '$default_url', '$verify', '')";
$result = @mysql_query($sql,$connection) or die(mysql_error());
}
//checks to see if the user needs to verify their email address before accessing the site
if ($verify == "0")
{
$mailheaders = "From: www$domain\n";
$mailheaders .= "Your account has been created.\n";
$mailheaders .= "Please activate your account now by visiting this page:\n";
$mailheaders .= "$base_dir/activate.html\n";
$to = "$_POST[/email][email]";
$subject = "Please activate your account";
mail($to, $subject, $mailheaders, "From: No Reply <$adminemail>\n");
}else{
header('Location:login.html');
}
?>[/PHP]
And after registration it sends you back to the login page. Ideally, after the newly registered user logs in, I wanted the "Welcome, $username" and other customized stuff, but that means passing session/cookie info through the register and login pages to get to the successfully-logged-in welcome page.
As usual, any help would be appreciated. I know I'm wading in a bit too deep here, but I'm a quick learner... I just can't find any tutorials on the subject matter, and all the dictionary-style spam on certain PHP sites doesn't help me understand how it actually works.[/email]
This is why sessions are commonly used on sites which are high security as it makes it harder to turn off and stay logged in, which is bad if you aren't using your own PC or if others can use it.
Cookies are saved on the users computer, meaning that even when the computer is restarted, they are still set.
The only way to destroy cookies is to delete them in your browser settings or have the website do it with some kind of logout script.
Current project: CMS Object.
Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
Release date: NEVER!!!
Can anybody tell me how to:
- Make a cookie that records the userid of the username that just successfully logged in, whether this action would be on the actual login.php or created on the page after that
- How to call info from that cookie (for instance, if your userid was 2, it would get userid 2 then query the database for all the info on userid 2, then if I did my "Welcome $username" it would have the necessary data and be able to display the username of userid 2. Am I getting this right? I think all of this is possible, I just can't find tutorials anywhere or anything explaining how to code this.)
I'll be trying the script you posted above, Nuvo, and try to learn cookieness from that, I believe I do have a working member script which can work together with it, but... I'm not certain about anything since what looks like it should work to me isn't always the case.
SET COOKIE:
GET COOKIE INFO:
THEN DELETE COOKIE: ]
replace all the stuff with your stuff
hope this helps