0

I am going crazy trying to learn how to use sessions to store values of custom fields in my registration_form.php so I can call the data on other pages. I have read all sorts of websites but nobody seems to explain where exactly I am supposed to put the code to capture the data. I have two custom registration fields I added to a script (bio and displayname). I tried inserting this code on the registration form at the top and bottom and also on a register.php (both scripts below).

Where does the code go to store these fields to a session? I know it is wrong below because at this point I have tried placing it everywhere in everyway I can....

//registration_form.php

<?php session_start();
$_SESSION['displayname'] = $displayname;
$_SESSION['bio'] = $bio;
$author = $_SESSION['displayname'];
$bio = $_SESSION['bio'];
?>
<HTML>
<head>
<title>Practice</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/style.css" rel="stylesheet" media="screen">
</head>
<body>
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<div class="logo">
<h2><?php include('db.php'); echo $logotxt; ?></h2>

</div>
<form class="form-horizontal" id="register_form" method="post">
<h2>Register</h2>

<div class="line"></div>
<div class="control-group">
<input type="text" id="inputEmail" name="email" placeholder="Email">
</div>
<div class="control-group">
<input type="text" id="inputuserid" name="username" placeholder="Username">
</div>
<div class="control-group">
<input type="text" id="displayname" name="displayname" placeholder="Display name">
</div>
<div class="control-group">
<textarea name="bio" class="textfield" id="bio" cols="25" rows="7" placeholder="Bio
(optional). Tell us about yourself."></textarea>

</div>         

<button type="submit" class="btn btn-large btn-primary btn-sign-in" data-loading-
text="Loading...">Register</button>
<a href="index.php" class="btn btn-large btn-register">Sign in</a>
<div class="messagebox">
<div id="alert-message"></div>
</div>
</form>

<?php 

// starting the session
session_start();

if (isset($_POST['Submit'])) { 
$_SESSION['displayname'] = $displayname;
$_SESSION['bio'] = $bio;
} 
?> 

//register.php

<?php
include("db.php");

$con=mysql_connect($server, $db_user, $db_pwd) //connect to the database server
or die ("Could not connect to mysql because ".mysql_error());

mysql_select_db($db_name)  //select the database
or die ("Could not select to mysql because ".mysql_error());

//prevent sql injection
$username=mysql_real_escape_string($_POST["username"]);
$displayname=mysql_real_escape_string($_POST["displayname"]);
$password=mysql_real_escape_string($_POST["password"]);
$email=mysql_real_escape_string($_POST["email"]);
$bio=mysql_real_escape_string($_POST["bio"]);

//check if user exist already
$query="select * from ".$table_name." where username='$username'";
$result=mysql_query($query,$con) or die('error');
if (mysql_num_rows($result))
{
die($msg_reg_user);
}
//check if user exist already
$query="select * from ".$table_name." where email='$email'";
$result=mysql_query($query,$con) or die('error');
if (mysql_num_rows($result))
{
die($msg_reg_email);

}

session_start();
$_SESSION['displayname'] = $displayname;
$_SESSION['bio'] = $bio;

$activ_key = sha1(mt_rand(10000,2222).time().$email);
$hashed_password = crypt($password); 
$query="insert into ".$table_name."(username,displayname,password,email,activ_key,bio)
values ('$username','$displayname','$hashed_password','$email','$activ_key','$bio')";

if (!mysql_query($query,$con))
{
die('Error: ' . mysql_error());
}
user5000
  • 129
  • 1
  • 9
  • did you try w3schools cos that's as basic as it can get..and you are using it very wrongly..go over there and try and figure it out..and if you can't let us know..we will help – 1baga Jul 05 '14 at 03:33
  • 1
    stay away from w3schools, they have dangerously bad code. –  Jul 05 '14 at 03:34
  • Yeah...but couldn't find my situation I have. I did figure out that it goes in the login.php script-not the registration.php script. Still doesn't work-now I have it like this: $query="select * from ".$table_name." where username='$username' and password='$hashed_password' and activ_status in(1)"; $result=mysql_query($query,$con) or die('error'); if(mysql_num_rows($result)) { $_SESSION['login'] = true; $_SESSION['username']=$username; $_SESSION['bio']=$bio; $_SESSION['displayname']=$displayname; echo json_encode( array('result'=>1)); } – user5000 Jul 05 '14 at 03:35
  • @Dagon then recommend a good place for him to start.. – 1baga Jul 05 '14 at 03:35
  • Yes, Dagon...I wasted the last 2 hours trying w3 schools. I know this is very basic, but it's my first time using sessions and it just won't work no matter what I have tried so far. – user5000 Jul 05 '14 at 03:38
  • @user3077135 try this page i think your solution is there [link](http://stackoverflow.com/questions/10097887/using-sessions-session-variables-in-a-php-login-script) – 1baga Jul 05 '14 at 03:39
  • Will do...thanks 1baga – user5000 Jul 05 '14 at 03:40
  • @user3077135 or again [this](http://www.sourcecodetuts.com/php/27/how-create-login-page-php-and-mysql-session) – 1baga Jul 05 '14 at 03:41
  • I did notice that one of my pages--the login form has this code at the top (just how script came that I am modifying): Can that be the problem? Why would we start a session and destroy it immediately? This is the same page where I am trying to store the session variables down lower after the sql insert... – user5000 Jul 05 '14 at 03:44

1 Answers1

0

I am not sure what you are trying to here but should not your form action in regisration_form.php be set to register.php?

<form class="form-horizontal" id="register_form" method="post" action="register.php">

Then you form will be submitted to register.php. The way you have set up Session in register.php is basically correct. To access the session value in other pages do:

$displayname= $_SESSION['displayname']; 
$bio=$_SESSION['bio']; 
user1111
  • 11
  • 5
  • Hi Ananta, that was how the code I purchased came. I tried changing the action to register.php and method post and I get the exact same result as leaving action out altogether. That is, it works both ways for everything except passing my custom variables. And besides, shouldn't the session variables be stored in the login form after a user logs in each time rather than in the registration form which is a one-time thing? – user5000 Jul 05 '14 at 04:23
  • Hi, The form is then probably submitted By Ajax. There must be javascript written that takes only email and username from your form and submits to register.php. Check for JS code and add code to read your custom fields as well. – user1111 Jul 05 '14 at 04:38
  • That's right...Ajax. I didn't know that impacted php sessions. If I need to manipulate JS to store the sessions then I am really in trouble as I know very little about javascript. Guess I'll start reading up. – user5000 Jul 05 '14 at 04:55