i have a sign up page.
when user signup ,his data moves to database'table having 4 colomns.1 is of username,2 of password, 3 of email & last for title.
when he signsup,a page choice.html opens that offers to login now or later.
if the user login at that time ,he is moved to a title.html page at which there is a txtbox & txtarea.
he types an article in txtarea & title in txtbox.
i want that title to be saved in the database'table ,in same row of the user.means it shoudl save in the row of that user who is now saving it.
but for this i have to compare the username in the table with one who is now logged in at page signup.there are also php pages in btw two html pages.
so will any body tell me please that how can i use session here for this.
or if there is any other method for this so kindly share.
regards

Recommended Answers

All 8 Replies

register a session variable username. Then use UPDATE tblename SET title=whatever WHERE username = '$_SESSION';

it is still not accessale.i have given a path to username session var to the 5th page.
there i equaled it to a var.then i aceessd it.bt still not wrking.
kindly hlp me.

<?php

session_start();
$_SESSION=$_POST;

?>
<a href="title.php">next</a>
i used this code to give it a path.is it right?

That looks right.

Remember you need to use session_start() on your other pages that you want access the variables.

i dont know whats going wrong.
no error occurs.it creates the txt file of article but do not send title to table.
i have another ques,when we upload an image,it moves to some folder,but i need this image to display on the textarea.means if user wants to upload an image ,it appears with the text.

:icon_lol:
<!--login.php -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Login</title>
    </head>
    <body >
    <script type="text/javascript">
        window.onload=function()
        {
            document.forms['login'].elements['username'].focus();
        }

    </script>

    <table align="left" cellpadding="10" cellspacing="2" border="2" bgcolor="#fedcaa">
        <form name="login" method="post" action="validate_log.php">
            <tr>
                <td>UserName </td>
                <td><input  type="text" name="username" id="username" /></td>
            </tr>

            <tr>
                <td>PassWord</td>
                <td><input  type="password" name="password" id="password" /></td>
            </tr>

            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="submit" id="submit" value="Connect" onblur="this.form.username.focus()" />
                </td>
            </tr>
        </form>
    </table>
    </body>
</html>
<!-- End Login.php
validate_log.php -->
<?php
if(isset($_POST['submit'])) {
    if(empty($_POST['username']) || empty($_POST['password'])) { 
    // Decide what to do if no username or password.
}
else {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $username = strtolower($username);
    // Open Database
    /*  define ("DB_SERVER", "Your Database Server"); (localhost?)
        define ("BD_USER", "Your username for connecting to your DataBase"); (root?)
        define ("DB_PASS", "Your password");
        $bdd = 'Your users table';
    */
    $db = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db($bdd) or die(mysql_error());

    $query = "SELECT *
              FROM   `your_mysql_table`
              WHERE  user='$username'";

    $result = mysql_query($query);
    $n = mysql_num_rows($result);
    if($n <= 0) {
        // Do whatever you want if the user isn't in your table
    }
    else {
        mysql_data_seek($result,0);
        $row = mysql_fetch_object($result);

        $username = trim($username);
        // Create a new session with SID = username
        session_id ($username);
        session_name($username);
        session_start(); 

        // Put all row values in the Superglobal array $_SESSION.
        foreach ($row as $key => $value) {
            $_SESSION[$key] = trim($value);
        }
        // Evantually, create all necessary session's variables you need.
        // Now you can go to whatever php page you want with the following:
        header('Location: your_destination.php?user='.$username);   
    }
}
// End validate_log.php
?>

<!--
At the beginning of each php page where you want to use session's variables, put the following lines:
<?php
if(!isset($_GET['user'])) {
    // Do what to do if there's no 'user' parameter
    //exit;
}
else {  
    $username = $_GET['user'] ;
    session_name($username);
    session_start(); // Open the current Session
    foreach ($_SESSION as $kay => $value) {
        $$key = $value; // Create a dynamic variable for each session's variable
    }
}
/*
...
The rest of your script.
*/
?>

<!--
Hope this will help you.
enjoy!
MP
-->
Member Avatar for diafol

Images don't show up in a textarea, if that's what you're trying to do - unless you have a modified textarea, like a WYSIWYG editor in place.

As mentioned, session variables can only be kept "live" if they are passed through every page/file. session_start() must be placed in each page - preferably at the top of each file in order to avoid and whitespace/html output related errors. This includes any form handling files that accept + process form data and then redirect to the next page.

@Horacio91 - please use code tags - v. difficult to read.

Hi,
Sorry for mistaken tags. Here's the correct post:

<!--login.php -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Login</title>
	</head>
	<body >
	<script type="text/javascript">
		window.onload=function()
		{
			document.forms['login'].elements['username'].focus();
		}

	</script>

	<table align="left" cellpadding="10" cellspacing="2" border="2" bgcolor="#fedcaa">
		<form name="login" method="post" action="validate_log.php">
			<tr>
				<td>UserName </td>
				<td><input  type="text" name="username" id="username" /></td>
			</tr>
				
			<tr>
				<td>PassWord</td>
				<td><input  type="password" name="password" id="password" /></td>
			</tr>
				
			<tr>
				<td colspan="2" align="center">
					<input type="submit" name="submit" id="submit" value="Connect" onblur="this.form.username.focus()" />
				</td>
			</tr>
		</form>
	</table>
	</body>
</html>
<!-- End Login.php
validate_log.php -->
<?php
if(isset($_POST['submit'])) {
	if(empty($_POST['username']) || empty($_POST['password'])) { 
	// Decide what to do if no username or password.
}
else {
	$username = $_POST['username'];
	$password = $_POST['password'];
	$username = strtolower($username);
	// Open Database
	/* 	define ("DB_SERVER", "Your Database Server"); (localhost?)
		define ("BD_USER", "Your username for connecting to your DataBase"); (root?)
		define ("DB_PASS", "Your password");
		$bdd = 'Your users table';
	*/
	$db = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
	mysql_select_db($bdd) or die(mysql_error());

	$query = "SELECT *
			  FROM	 `your_mysql_table`
			  WHERE	 user='$username'";
	
	$result = mysql_query($query);
	$n = mysql_num_rows($result);
	if($n <= 0) {
		// Do whatever you want if the user isn't in your table
	}
	else {
		mysql_data_seek($result,0);
		$row = mysql_fetch_object($result);

		$username = trim($username);
		// Create a new session with SID = username
		session_id ($username);
		session_name($username);
		session_start(); 
		
		// Put all row values in the Superglobal array $_SESSION.
		foreach ($row as $key => $value) {
			$_SESSION[$key] = trim($value);
		}
		// Evantually, create all necessary session's variables you need.
		// Now you can go to whatever php page you want with the following:
		header('Location: your_destination.php?user='.$username);	
	}
}
// End validate_log.php
?>

<!--
At the beginning of each php page where you want to use session's variables, put the following lines:
<?php
if(!isset($_GET['user'])) {
	// Do what to do if there's no 'user' parameter
	//exit;
}
else {	
	$username = $_GET['user'] ;
	session_name($username);
	session_start(); // Open the current Session
	foreach ($_SESSION as $kay => $value) {
		$$key = $value; // Create a dynamic variable for each session's variable
	}
}
/*
...
The rest of your script.
*/
?>

<!--
Hope this will help you.
enjoy!
-->
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.