I have created a form for a quiz, where you have to enter the answers and a PHP script will total them and send them to en E-Mail address and save them to a file. However, the user is only redirected on the 2nd attempt (submission) of the form. The results are totalled and sent on the first submission, but it does not redirect. On the 2nd submission, the results are not totalled, but an e-mail is sent and the user is redirected to the completion page.
Here is my code:-
<!DOCTYPE html>
<head>
<title>Quiz</title>
<link rel="stylesheet" type="text/css" href="../styles.css" />
<link rel="shortcut icon" href="../img/favicon.ico" type="image/x-icon" />
</head>
<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
if (isset($_SESSION['username'])) $username = $_SESSION['username'];
else header("Location: http://quiz.dibdibguy.com/client/index");
include_once($_SERVER['DOCUMENT_ROOT']."/scripts/sql.php");
$qid = $_SESSION['quiz'];
$con = connect();
$data = mysqli_query($con, "SELECT * FROM `qs` WHERE `id`='$qid' LIMIT 1");
$qinfo = mysqli_fetch_array($data, MYSQLI_ASSOC);
?>
<body><div id="white"> </div>
<div id="content" class="text">
<div class="header">Quizzes</div>
<div class="subheader">Quiz: <?php echo($qinfo['name']); ?></div>
<table width="85%" class="menu text" align="center">
<tr>
<td>
<div align="center"><?php if ($qinfo['desc'] == NULL) echo($qinfo['name'] . ", has no description available."); else echo($qinfo['desc']); ?></div>
</td>
</tr>
</table>
<br>
<?php
$table = $qinfo['table'];
$questionsTable = mysqli_query($con, "SELECT * FROM `$table`");
?>
<table width="85%" class="menu text" align="center">
<tr>
<td>
<div align="left"><b>Please Remember:</b> For questions that require you to type an answer, you must spell it correctly, as the system cannot detect spelling errors.<br>
<b>Playing as: <i><?php echo($_SESSION['username']); ?></i></b></div>
</td>
</tr>
</table>
<br>
<table width="85%" class="menu text" align="center">
<tr>
<td>
<form method="post" name="quiz" action="">
<?php
$amt = mysqli_num_rows($questionsTable);
$count = 1;
echo($questions['que']);
while($ques = mysqli_fetch_array($questionsTable)){
echo("Question: " . $ques['que'] . "<br>");
echo("Answer: <input type=\"text\" name=\"$count\"><br><br>");
$count = $count + 1;
}
?>
<input type="submit" name="submit" value="Submit Answers" id="btn">
</form>
</td>
</tr>
</table>
<?php
if (isset($_POST['submit'])){
$answers = mysqli_query($con, "SELECT `ans` FROM `$table`");
$u_answers = $_POST;
$correct = 0;
while($answer = mysqli_fetch_assoc($answers)){
echo("Hi");
foreach($u_answers as $u_ans){
if ($answer['ans'] == strtolower($u_ans)) $correct = $correct + 1;
$count = $count + 1;
}
}
header("Location: http://quiz.dibdibguy.com/client/index");
#WRITE DATA TO FILE
$file = fopen("../results/" . strtoupper($_SESSION['username']) . "_" . date("d-m-Y_h:i_sa") . "_" . strtoupper($_SESSION['quiz'] . ".txt"), 'w');
fwrite($file, ($correct) . "/" . ($amt));
fclose($file);
mail("aaron#dibdibguy.com", $_SESSION['username'] . "quiz results", $correct . "/" . $amt);
$_SESSION['quiz'] = $qinfo['name'];
}
close($con);
?>
<br>
<?php include "../footer.php"; ?>
<br>
<br>
</div>
</body>
</html>
On the 1st attempt, if I get 1 question correct, I receive an e-mail stating; '1/39', which is what should happen, but on the 2nd attempt, I get an e-mail stating '0/', even if I get some correct.
Thanks in advance for any assistance. If you need anything else, please, E-Mail me (aaron#dibdibguy.com), or comment on this question!
Web Host: unlimitedwebhosting
PHP Version: 5.5
I see two problems.
First - HTML headers have to be sent before any content is sent. If you look in your error logs there are likely warnings about this. You print quite a lot of html to the page before your location header is used for the redirect. Since it's too late for headers it's ignored (you don't get redirected). To fix this either move the redirect logic further up the page before any output (and plain HTML counts as output) or use output_buffering to keep the output from being sent to the browser until you've sent all your headers.
Second - The '0/' email seems to be an error in your logic. If you look at this block:
if (isset($_POST['submit'])){
... Other Stuff
mail("aaron#dibdibguy.com", $_SESSION['username'] . "quiz results", $correct . "/" . $amt);
... More Stuff
}
The logic you've written says that the email will be sent any time the submit button is pressed. You never did a 'sanity check' to see if you actually have any valid information in the form first.
Related
I have this program that is kinda like a chat app "for testing purposes"
It works correctly but when i send a message i have to reload to make it appear on my end. I tried to redefine the text in the database after i send the message but it didnt work. I dont want to add a refresh button (i will add just to make it easier to check for if someone wrote something) , but i want the message to appear after i write it. So thought to add a refresh header in php sadly it always said that the headers were modified somewhere before so i added it before that code with a 1 sec delay , it worked but it loops. Is there any place to add the refresh header or do you have a better solution?
<?php
ini_set('session.use_cookies', 0);
$file_pointer = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$dbc = file_get_contents($file_pointer);
?>
<form action="chat.php" method="post">
<title>Sm Chat</title>
<link rel="stylesheet" type="text/css" href="stylechat.css">
<head>
<div class="nazi">
<a style="text-decoration:none" href="index.html"> Home </a>
</div>
</head>
<body vlink='white' alink='white' link='white'>
<center>
<div class="cont">
<?php
echo "<br>" . $dbc
?>
</div>
</center>
<center>
<?php
ini_set('session.use_cookies', 0);
if(isset($_POST['btn']))
{
$msg = $_POST['msg'];
$usrfile = "usr.txt";
$usr = file_get_contents($usrfile);
$raw = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$fp = fopen( $raw, 'r+');
$messg = $dbc . $usr . " : " . $msg . "<br>";
fwrite($fp,$messg);
fclose ($fp);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
}
?>
<div class="input-form">
<input type="text" value="" id="msg" name="msg" placeholder="Enter Your Message"/>
</div>
<input type="submit" value="Send" name="btn" class="btn"/>
</center>
Its best to place all PHP code on top of your page and your HTML under it.
Your HTML is not correct at all, always start with the <html> tag and add a <head> and a <body>.
I structurized your code.
You should add your refresh header as high as possible before any output is generated and also inside the $_POST statement to ensure no output is generated before it.
You should also remove all post values before refreshing to prevent the loop.
Take a look at this reference:
https://www.php.net/manual/en/function.header.php
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
This is how your code should look like:
<?php
ini_set('session.use_cookies', 0);
$file_pointer = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$dbc = file_get_contents($file_pointer);
if(isset($_POST['btn']))
{
$msg = $_POST['msg'];
$usrfile = "usr.txt";
$usr = file_get_contents($usrfile);
$raw = "../../programs/chat-database/deb570314ba42230d7f5493b57b53970/driver.sys";
$fp = fopen( $raw, 'r+');
$messg = $dbc . $usr . " : " . $msg . "<br>";
fwrite($fp,$messg);
fclose ($fp);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
$dbc = file_get_contents($raw);
unset($_POST); // remove all post values
header("Location: yourpage.php"); // your refresh header
}
?>
<html>
<head>
<title>Sm Chat</title>
<link rel="stylesheet" type="text/css" href="stylechat.css">
</head>
<body vlink='white' alink='white' link='white'>
<div class="nazi">
<a style="text-decoration:none" href="index.html"> Home </a>
</div>
<center>
<div class="cont">
<?php
echo "<br>" . $dbc;
?>
</div>
</center>
<center>
<form action="chat.php" method="post">
<div class="input-form">
<input type="text" value="" id="msg" name="msg" placeholder="Enter Your Message"/>
</div>
<input type="submit" value="Send" name="btn" class="btn"/>
</form>
</center>
</body>
</html>
I've been trying to implement the following shopping cart website on xampp. The link for the page is : http://www.w3programmers.com/build-a-shopping-cart-with-php-part-1/
However when I try running my login page I get the following error:-
Here's the tab that opens when I try to login:
Here's the login code :-
<?php
session_start();
require("config.php");
if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
header("Location:".$config_basedir);
}
if($_POST['submit'])
{
$loginsql = "SELECT * FROM logins WHERE username = '" . $_POST['userBox']. "' AND password = '" . $_POST['passBox'] . "'";
$loginres = mysql_query($loginsql);
$numrows = mysql_num_rows($loginres);
if($numrows == 1)
{
$loginrow = mysql_fetch_assoc($loginres);
session_register("SESS_LOGGEDIN");
session_register("SESS_USERNAME");
session_register("SESS_USERID");
$_SESSION['SESS_LOGGEDIN'] = 1;
$_SESSION['SESS_USERNAME'] = $loginrow['username'];
$_SESSION['SESS_USERID'] = $loginrow['id'];
$ordersql = "SELECT id FROM rides WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status < 2";
$orderres = mysql_query($ordersql);
$orderrow = mysql_fetch_assoc($orderres);
session_register("SESS_ORDERNUM"); $_SESSION['SESS_ORDERNUM'] = $orderrow['id'];
header("Location:".$config_basedir);
}
else {
header("Location:http://" .$_SERVER['HTTP_HOST']. $_SERVER['SCRIPT_NAME'] . "?error=1");
}
}
else {
require("header.php");
?>
<h1>Customer Login</h1>
Please enter your username and password to log into the websites. If you do not have an account, you can get one for free by registering.
<?php
if(isset($_GET['error'])) {
echo "<strong>Incorrect username/password</strong>";
}
?>
<form action="<?php $_SERVER['SCRIPT_NAME']; ?><br />" method="POST">
<table>
<tbody>
<tr>
<td>Username</td>
<td><input type="textbox" name="userBox" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="passBox" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Log in" /></td>
</tr>
</tbody>
</table>
</form>
<?php
}
require("footer.php");
?>
And here's the header code:-
<?php
session_start();
if(isset($_SESSION['SESS_CHANGEID'])==TRUE){
session_unset();
session_regenerate_id();
}
require("config.php");
$db = mysql_connect($dbhost, $dbuser, $dbpassword);
mysql_select_db($dbdatabase, $db);
?>
<head>
<title><?php echo $config_sitename; ?></title>
<meta charset="utf-8"/>
<link rel="stylesheet" type="text/css" href="../stylesheet.css">
</head>
<body>
<div id="header">
<h1><?php echo $config_sitename; ?></h1>
</div>
<div id="menu">
Home
View Rides
</div>
<div id="container">
<div id="bar">
<?php
require("bar.php");
echo "<hr>";
if(isset($_SESSION['SESS_LOGGEDIN'])==True){
echo "Logged in as <strong>".$_SESSION['SESS_USERNAME']."</strong>[<a href='".$congif_basedir."logout.php'>logout</a>]";
}else{
echo "<a href='".$config_basedir."login.php'>Login</a>";
}
?>
</div>
<div id="main">
I suspect it's something to do with the header function, though I'm not really sure.
May I ask you to put here code from config.php? Or at least tell what value of $config_basedir?
Updated: $config_basedir should be URL in this format: http://localhost/ . 'some folders' . 'file name with extension', for example, like this: http://localhost/sites/shop/index.php.
If a file or folder of the file is located in the same or child directory your URL can be just: file 'file_name or folder/file_name. For example just index.php or folder/folder/index.php.
An extension like .php | .htmlmay not be mandatory depending on your server settings. That is, you can indicate the URL like this: folder/index or index; 'index' - is a file name in this case.
Hey guys this is my first PHP project so I'm a rookie and I'm developing a forum website but I need to develop comment system here so I'm using the following code to accept the values from my HTML form which then assigns these values to two PHP variables and they call a function which I have stored in a different PHP file.
Now the problem is that these values are being submitted automatically whenever I refresh my webpage so my database is getting filled up with the same values that I assigned to those variables so I need your help in sorting this out.
<html>
<head>
<?php include 'menu.php'?>
<?php include 'include/core.php'?>
<?php
if($_SERVER['REQUEST_METHOD']=="POST")
{
$nme = trim($_POST['name']);
$cmnt = trim($_POST['comments']);
if(!empty($nme) && !empty($cmnt))
{
create_commentsystem($nme,$cmnt);
unset($nme);
unset($cmnt);
}
else
{
echo "Please enter the complete details";
unset($nme);
unset($cmnt);
}
}
?>
<link href="css/main.css" rel="stylesheet" type="text/css">
<link href="css/auth.css" rel="stylesheet" type="text/css">
<title>Doctors Forum</title>
</head>
<body>
<div id="container">
<div class="tab" align="Center">
Page Content Here
</div>
<div class="content">
<form method="post" name="form1" action="">
<input class="field" type="text" name="name" placeholder="Name" style="width:635px; height:40px;"/></br></br>
<textarea class="field" name="comments" placeholder="Leave Comments Here..." style="width:635px; height:100px;"></textarea></br></br>
<input class="btn" type="submit" value="Post" style="width:150px;" >
</form>
</div>
<div id="" class="tab" align="center">
<div>
</div>
</body>
</html>'
And here is the Function that is being called when I press the button and the problem is the same record is being inserted exactly twice every time i reload the page
` function create_commentsystem($name,$comments)
{
$name = $_POST['name'];
$comments = $_POST['comments'];
$conn = dbConnect();
mysqli_query($conn, "INSERT INTO comments(name, comments) VALUES('$name','$comments')");
$result = mysqli_query($conn, "SELECT * FROM comments ORDER BY id ASC");
while($row=mysqli_fetch_array($result))
{
echo "<div class='comments_content'>";
echo "<h4><a href='delete_commentsystem()?id=" . $row['id'] . "'> X</a> </h4>";
echo "<h1>" . $row['name'] . "</h1>";
echo "<h2>" . $row['comments'] . "</h2></br></br>";
echo "<h3>" . $row['date_publish'] . "</h3>";
echo "</div>";
}
$conn->close();
}`
When you refresh the web page the browser sends the request again. The easiest solution is after saving the comment to use header to redirect.
Example:
if (add_comment_success()){
header('Location: http://www.current.url.com/');
exit();
}
Two things can be done:
Re-direct as suggested by other answers
If re-direct is not an option, check in DB to see if same name and comment
exists and dont add it
There is no other way of preventing this.
You could try appending
'?submit=true'
to your action url. Use
$_GET['submit']
to access the value of 'submit'. Your code could look like this
if($_GET['submit'] == true):
#form processing code
$_GET['submit'] = false;
header('your original file');
endif;
I am trying to implement a page where a user enters a comment and it gets displayed right in the same page. The problem i am having is that every time you go to the page there are no comments in the page(there are actually comments).
This is my sceneario i am having:
I go to the page and there are no comments, i enter a comment 'hello' and it gets displayed right away.
I go to a different page and then i come back to the comments page and there are no comments.(the comment "hello" should be already displayed)
I enter a comment "hi" and both comments "hello" and "hi" get displayed
I cant resolve this issue..
This is my code, its pretty long
<?php
session_start(); //starts or continues the session
require_once('functions.php'); //needed for some function calls
error_reporting(E_ALL ^ E_NOTICE);
?>
<!DOCTYPE html>
<html lang = "en">
<head>
<script type = "text/javascript" src = "functions.js"></script>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body>
<?php
GetUserLayout($_SESSION['userId'], $_SESSION['superUser']);
?>
<div id = "shareyouridea_form" class = "post">
<h1> Share your post</h1>
<!-- used for the form -->
<form id = "idea_form" method = "post"
action = "<?php echo $PHP_SELF;?>"
onkeypress = "return DisableEnterKey(event);">
<table>
<caption>
<strong>
<br /> Share post form:
</strong>
</caption>
<tr class = "spacearound"> <!-- input for bright idea -->
<td> Post: </td>
<td>
<textarea form = "idea_form" name = "b_idea" rows = "12"
cols = "85" title = "Please describe your product idea"
id = "bright_idea" maxlength = "1000"
onkeypress =
"return InputLimiter(event, 'lettersSpacePunctuation');">
</textarea>
</td>
</tr>
</table>
<p>
<input type = "reset" value = "Reset" />
<input type = "submit" value = "Share Idea!"
title = "complete form first to submit"
id = "submit_button"
name = "add_comment"
onmousedown = "IsIdeaFormCompleted();" />
</p>
</form> <!-- end idea_form -->
</div>
</div> <!-- end of ShareYourIdea_middle -->
<script>
DisplayFooter();
</script>
<?php
if(isset($_POST['add_comment'])){ // if add comment was pressed
// get variables
$name = $_SESSION['firstName'];
$empId = $_SESSION['userId'];
$idea = $_POST['b_idea'];
// CONNECTING TO OUR DATABASE
$db = mysqli_connect(dbHost, dbUser, dbPassword, dbName);
if (mysqli_connect_errno()) { //if connection to the database failed
echo("<p id = 'greatideadescription'>
Connection to database failed: " .
mysqli_connect_error($db) . "</p>");
exit("goodbye");
} //by now we have connection to the database
// WE WRITE OUR QUERY TO INSERT POST INFO TO DATABASE
$query = "INSERT INTO posts(postId,empl_Id,post,postDate)
VALUES('','$empId','$idea',NOW())";
$result = mysqli_query($db, $query);
}
?>
<?php
// WE DO A QUERY TO SHOW ALL COMMENTS IN THE PAGE
$query = "SELECT firstName,lastName, post,
date_format((date_add(postDate,interval -7 hour)),'%a, %M, %d, %Y at %I:%i%p' ) as mydatefield
FROM users INNER JOIN posts ON userId = empl_Id
ORDER BY postDate DESC";
$result = mysqli_query($db,$query);
if (!$result) { //if the query failed
echo("<p id = 'greatideadescription'>
Error, the query could not be executed: " .
mysqli_error($db) . "</p>");
mysqli_close($db);}
if (mysqli_num_rows($result) == 0) { //if no rows returned
echo("<div id = 'blogs'>
<div id ='name'>
No posts detected
</div>
</div>
<div class='fb-like' data-href='http://jacobspayroll.zxq.net/index/blog.php' data-send='true' data-width='450' data-show-faces='true'></div>
");
mysqli_close($db); //close the database
exit("</table></div></form></div></div>
<script>DisplayFooter();</script></body></html>");
} //by now we know that we have some products purchases returned
$numRows = mysqli_num_rows($result); //gets number of rows
$numFields = mysqli_num_fields($result); //gets number of fields
//prints the data in the table
while($row = mysqli_fetch_assoc($result)){
$posted = $row['post'];
$message = wordwrap($posted,5);
echo
'<div id ="blogs">
<table id = "blog_id">
</br>
<div id = "name">
<strong>'.$row['firstName'] . ' ' .$row['lastName'].
'</strong>
: ' .$message .
'<br/>
</div>
<div id ="date">'.
$row['mydatefield'] . '
</div>
<div id ="delete_comment">
Delete this comment
</div>
<p>
</table>
</div>';
}
mysqli_close($db);
?>
</body>
</html>
You have the wrong Usage of PHP_SELF
//You must use Server and execution environment information `$_SERVER[]`
$_SERVER['PHP_SELF'];
// For your form action like this
action = "<?php echo $_SERVER['PHP_SELF'];?>"
as Kail mentioned you got it wrong but you might want to use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['PHP_SELF'] then you might want to add some script to get GET parameters if you use them for your script(s). If you use PHP_SELF you might have a user link to script.php/%22%3E%3Cscript%3Ealert('xss')%3C/script%3E%3Cfoo might look like action="script.php/"><script>alert('xss')</script> or could be a redirect to collect cookies and the alike in other words XSS attack.
$_SERVER['PHP_SELF'] vs $_SERVER['SCRIPT_NAME'] vs $_SERVER['REQUEST_URI']
XSS Woes
What's the difference between $_SERVER['PHP_SELF'] and $_SERVER['SCRIPT_NAME']?
I think It is not a hard problem, but I was stuck in it.
Currently I built up a log in and sign up(registration) interface using php.
My thought is direct:
index.php shows "log in" and "sign up", if you do not have an account in database, you are led to "sign up" page, after filling out all information needed, I would like to direct the user to a page which shows "you are registered as ". The problem is here, I tried to use $_SESSION['username'] to correspond to who the user is. But unfortunately no username is print out. I used isset() in registered.php to check if $_SESSION['username'] is set. It shows it is not set, but it should be there!!!
The flow line is signup.php -> register.php -> registered.php. My question is $_SESSION['username'] should be passed down in the php files after declaration of session_start(). Below is my code, would some one point out where I went wrong.
Thanks a lot.
signup.php
<?php
session_start(); // attention add session_start() again, am I wrong?
$title = "Sign up";
print_r($_SESSION);
?>
<html>
<head>
<title><?= $title ?></title>
<!--
<link href="index.css" type="text/css" rel="stylesheet"></link>
-->
<link href="signup.css" type="text/css" rel="stylesheet"></link>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div id="main-content">
<form method="post" action="register.php">
<label><input class="defaultText" title="username" name="username" type="text"></label><br/>
<label><input class="defaultText" title="email" name="email" type="text"></label><br/>
<label><input class="defaultText" title="company" name="company" type="text"></label><br/>
<label class = "password">password:<input name="pass1" type="password" /></label><br/>
<label class = "password">password (again):<input name="pass2" type="password"/></label><br/>
<input type="submit" value="Sign up!">
</form>
<div style="clear: both;"></div>
</div>
</body>
</html>
register.php
<?php
if(isset($_REQUEST['username']) && isset($_REQUEST['email']) && isset($_REQUEST['company']) && isset($_REQUEST['pass1']) && isset($_REQUEST['pass2'])){
$username = $_REQUEST['username'];
$email = $_REQUEST['email'];
$company = $_REQUEST['company'];
$pass1 = $_REQUEST['pass1'];
$pass2 = $_REQUEST['pass2'];
if(strlen($username) <= 0) {
die("Username must be greater than 0 characters\n");
}
if(strlen($email) <= 0) {
die("Email must be greater than 0 characters\n");
}
if(strlen($company) <= 0) {
die("Company name must be greater than 0 characters\n");
}
if($pass1 != $pass2 && $pass1 <= 0) {
die("Passwords are not the same.");
}
$hash = hash('sha256', $pass1);
# connect to world database on local computer
check(mysql_connect("localhost", "root", "123456"), "connect");
$connect = mysql_connect("localhost", "root", "123456");
# in order to insert data into database tables, database is already there built via mySQL command from terminal
# connect with the database
check(mysql_select_db("product"), "selecting db");
# mysql_real_escape_string() This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.
$username = mysql_real_escape_string($username);
$email = mysql_real_escape_string($email);
$company = mysql_real_escape_string($company);
# this $query query is to send the "insert values" query into a table
$query = "INSERT INTO admins ( username, hash, email, company ) VALUES ( '$username', '$hash', '$email', '$company' );";
$results = mysql_query($query);
check($results, "adding user"); // if not pass show error message
header("Location: registered.php");
}
function check($result, $message) {
if (!$result) {
die("SQL error in $message: " . mysql_error());
}
$_SESSION['username'] = $_REQUEST['username'];
$_SESSION['email'] = $_REQUEST['email'];
$_SESSION['company'] = $_REQUEST['company'];
}
?>
registered.php
<?php
$title = "Registered!";
?>
<html>
<head>
<title><?= $title ?></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<?php
print_r($_SESSION);
if(isset($_SESSION['username']))
print "hello";
print $_SESSION['username'];
echo $_SESSION['company'];
?>
<div id="main-content">
<h3>You have registered in administration system as <?= $_SESSION['username'] ?></h3>
<table border="1">
<tr>
<th>Name</th>
<th>Company</th>
<th>Email</th>
</tr>
<tr>
<td><?= $_SESSION['username'] ?></td>
<td><?= $_SESSION['email'] ?></td>
<td><?= $_SESSION['company'] ?></td>
</tr>
<h3>please log in into the administration system</h3>
<h4>Log In</h4>
</table>
<div style="clear: both;"></div>
</div>
</body>
</html>
You need to add session_start(); on every page before any content.
register.php and registered.php are missing the session starter and they both use $_SESSION
Edit: like Mike Brant said, make a global file which starts the session and include it in every file before any content. That way you can have all your common variables and session in a single file.