2

I am having trouble trying to pass my username variable from a login page to the next 'welcome' page. I am hosting the web pages using my university's server and therefore access my web files within a public_html folder.

When testing on the login.php page I am able to print the username that is held in the session however when I try the same in the member's page it does not print the username held in the session array? I have checked all other posts however no solution seems to work.

Login.php

<?php
session_start();
include ('connect.php');
$userName=$_POST['username']; 
$password=$_POST['password'];  
$result = mysql_query("SELECT * FROM users WHERE username='$userName' AND password='$password'") or die ('Query is invalid: ' . mysql_error());;
$count =  mysql_num_rows($result);

if($count==1)
{
$_SESSION['username']=$userName;
header("Location: memberPage.php");
} else {
echo "Incorrect username or password";
}
?>

memberLogin.php

<?php
session_start();
include ('connection.php');

$_SESSION['username'] = $userName;
echo $userName;

//$result = mysql_query("SELECT * FROM users");
//while ($row = mysql_fetch_array($result)) {
//    echo $row['username'];
//}
?> 

<html>
<link rel="stylesheet" type="text/css" href="style.css" />

<div id = main-nav>
<a href="http://www.cs.nott.ac.uk/~rxp00u/logout.php" >Logout</a> 
</div>
</html>

P.S. I realise my code is not very secure and I will be working on this after getting the basic framework of the website working!

EDIT: Error reporting on login.php shows the following error: "Warning: Unknown: open(/var/lib/php5/sess_d760m9ebgose4liptp6jbgbc17nqjgg4, O_RDWR) failed: Permission denied (13) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0"

3 Answers 3

1
<?php
session_start();
include ('connection.php');

$_SESSION['username'] = $userName;
echo $userName;

$userName is not set. If you had errors on you'd have seen that. Did you mean

$userName = $_SESSION['username'];
echo $userName;

?

Sign up to request clarification or add additional context in comments.

10 Comments

thanks, i tried this however still no output of the username that is logged in
just try replacing header("Location: memberPage.php"); with a meta refresh echo '<meta http-equiv="refresh" content="0;URL=memberPage.php">'; - it's a cookie thing
thanks, that redirected me back to memberPage but nothing still shows on the memberPage.php
Hi, after turning errors on I have seen that I have errors when writing my sessions to the file. I am on my university's server so have the web files within a public_html folder so am not sure where the php files would be held? Here is the error that was outputted in my login.php file: "Warning: Unknown: open(/var/lib/php5/sess_d760m9ebgose4liptp6jbgbc17nqjgg4, O_RDWR) failed: Permission denied (13) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0"
ok for now make your own session dir and use it php.net/manual/en/function.session-save-path.php
|
1

on memberLogin.php you are setting:

$_SESSION['username'] = $userName;

where $userName is undefined. it appears that if you can successfully assign $userName to a $_SESSION variable on Login.php, you no longer need to set the variable. It will be accessible once you call session_start();.

2 Comments

if i do not need to set the variable again how would i access $userName in memberPage.php?
$userName = $_SESSION['username'];
0

In memberLogin, it should be:

$username = $_SESSION['username'];

Assignment is left associative, meaning the value on the right of the = is assigned to the variable on the left of the =. Your current setup has you overwriting your session value with null.

4 Comments

thanks i tried this but no luck with any output of the session's username
Do some basic debugging to narrow the problem: Do you have error reporting turned on? Have you tried running your query in phpMyAdmin? Can you var_dump $username in login.php? Can you var_dump $_SESSION['username'] after the assignment in login.php?
I turned on error reporting and found errors when trying to write session data. I am currently on my university's machine and so accessing my web files would be held in my public_html folder. The error was as follows: "Warning: Unknown: open(/var/lib/php5/sess_d760m9ebgose4liptp6jbgbc17nqjgg4, O_RDWR) failed: Permission denied (13) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/var/lib/php5) in Unknown on line 0"
Ah, well there's your problem. Edit your question with those details. Be sure to mention you're on a university server. That's pertinent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.