...

View Full Version : Stored session variables



SmoothieVemund
11-03-2010, 08:14 AM
Hay.

I'm coding a social network thingy, and I'm having some problems. What I'm trying to do; If user is logged in there is a stored session variable who keep them logged in until the logout. And when they are logged in they can see the textarea were they can type "What's on your mind", and if they do that it works perfectly. But in the database it won't save who posted that massage, and that's the problem. Let me show you.


<?php
session_start(); // Must start session first thing
/*
Created By Adam Khoury @ www.flashbuilding.com
-----------------------June 20, 2008-----------------------
*/
// See if they are a logged in member by checking Session data
$toplinks = "";
if (isset($_SESSION['id'])) {
// Put stored session variables into local php variable
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$toplinks = '<a href="member_profile.php?id=' . $userid . '">' . $username . '</a> &bull;
<a href="member_account.php">Account</a> &bull;
<a href="logout.php">Log Out</a>';
} else {
$toplinks = '<a href="join_form.php">Register</a> &bull; <a href="login.php">Login</a>';
}
$postmassage = "";
if (isset($_SESSION['id'])) {
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$postmassage = ' <form action="post_massage.php" method="post">
<textarea name="post" cols="55" rows="8" id="post">Whats on your mind today?</textarea><br />
<input type="submit" />
</form>';
} else {
$postmassage = 'To write a massage, you need to be <a href="login.php">logged in</a>.';
}
?>
<?php
// Use the URL 'id' variable to set who we want to query info about
$id = ereg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for security
if ($id == "") {
echo "Missing Data to Run";
exit();
}
//Connect to the database through our include
include_once "connect_to_mysql.php";
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM members WHERE id='$id' LIMIT 1");
$count = mysql_num_rows($sql);
if ($count > 1) {
echo "There is no user with that id here.";
exit();
}
while($row = mysql_fetch_array($sql)){
$country = $row["country"];
$state = $row["state"];
$city = $row["city"];
$accounttype = $row["accounttype"];
$bio = $row["bio"];
// Convert the sign up date to be more readable by humans
$signupdate = strftime("%b %d, %Y", strtotime($row['signupdate']));
}
?>
<html>
<head>
<title>MyBubble - The new thingy</title>
</head>
<body>
<h1>MyBubble!</h1>
<div id="nav"><?php echo $toplinks; ?></div>
<br />
<?php echo $postmassage; ?>
<?php
include ("includes/includes.php");

$blogPosts = GetBlogPosts();

foreach ($blogPosts as $post)
{

echo "<p>" . $post->post . "</p";
echo "<span class='footer'><b>Posted By: " . $post->author . " Posted On: " . $post->datePosted . "</b></span";
}
?>
</body>
</html>
Here "<?php echo $postmassage; ?>" is the textarea with the form.


<?php
include_once "connect_to_mysql.php";
session_start();
$username = "";
if (isset($_SESSION['id'])) {
// Put stored session variables into local php variable
$userid = $_SESSION['id'];
$username = $_SESSION['username'];
$username = $userid;
} else {
$username = 'You need to be logged in and registered to post a new massage. Click here to <a href="login.php">login</a> or <a href="join_form.php">register</a>. . </a>';
}
?>
<?php
$con = mysql_connect("localhost","root","annelin38");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("blog", $con);

$sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
VALUES
('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo 'Your massage have been submitted. <br /><br />
To return to your profile, <a href="member_account.php">click here</a>';
exit();

mysql_close($con)
?>
When they have posted, it must be sent to the database. Here is the problem.

Any idea?

abduraooft
11-03-2010, 10:34 AM
$sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
VALUES
('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')"; What do you get for

echo $sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
VALUES
('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')"; ?

SmoothieVemund
11-03-2010, 04:40 PM
What do you get for

echo $sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
VALUES
('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[$username]')"; ?



Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\Programfiler\Xampp for Windows\xampp\htdocs\post_massage.php:2) in D:\Programfiler\Xampp for Windows\xampp\htdocs\post_massage.php on line 4
INSERT INTO blog_posts (id, post, author_id, date_posted) VALUES ('','Whats on your mind today?','','')Your massage have been submitted.

To return to your profile,

And btw. The thing I want to do is to get to say who is logged in and writes the massage typed.

$userid = $_SESSION['id'];
$username = $_SESSION['username'];
I have those session variables to do that.

SmoothieVemund
11-03-2010, 04:45 PM
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at D:\Programfiler\Xampp for Windows\xampp\htdocs\post_massage.php:2) in D:\Programfiler\Xampp for Windows\xampp\htdocs\post_massage.php on line 4
INSERT INTO blog_posts (id, post, author_id, date_posted) VALUES ('','Whats on your mind today?','','')Your massage have been submitted.

To return to your profile,

And btw. The thing I want to do is to get to say who is logged in and writes the massage typed.

$userid = $_SESSION['id'];
$username = $_SESSION['username'];
I have those session variables to do that.
Someting like this if you understand:

$sql="INSERT INTO blog_posts (id, post, author_id, date_posted)
VALUES
('$_POST[id]','$_POST[post]','$_POST[date_posted]','$_POST[author_id = $userid]')";



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum