Jump to content

Recommended Posts

I am having trouble with basic PHP sessions - I've checked out all the tutorials and everything but just can't figure out what else to do or what is wrong...

signin.php
[code]
<?php include("head.html"); ?>
</center></font></b>
<font size="3" color="red">
<?php
$error = $_GET['error'];
$signin = $_GET['signup'];

if ($error == invalid){
echo 'Invalid password/username combination';
}
if ($signin == true){
echo 'Congratulations!  You have successfully signed up!';
}
?>
</font><b><font size="4"><center>
Sign-in
<?php include("font.html"); ?>
<br><br>Type in your login information.  If you do not yet have an account, please <a href="signup.php">request an invite</a>.<br><br>
<form method="POST" action="signinprocess.php">
Username:<br>
<input type="text" name="username" size="25" maxlength="20"><br>
Password:<br>
<input type="password" name="password" size="25" maxlength="20"><br>
<input type="submit" value="Sign In" name="submit"></p>
</form>

<?php include("foot.html"); ?>
[/code]

signinprocess.php
[code]
<?php
session_start();
$_SESSION["username"] = "$username";
$username = $_POST['username'];
$password = $_POST['password'];

// make mysql connection - the stuff below was changed on purpose for the posting of this thread
mysql_connect("*SERVER*", "*USERNAME*", "*PASSWORD*") or die(mysql_error());
mysql_select_db("*DATABSE*") or die(mysql_error());

//must use $emailpass stored in db because password is encrypted (md5)
$query = "SELECT * FROM hsmemberdb WHERE username='$username' and emailpass='$password'";

$result = mysql_query($query);

//if there is anything but one unique entry - error in signin
if (mysql_num_rows($result) != 1) {
include("signin.php?error=invalid");
exit();
}else {
header('Location: memers/index.php');
}
?>
[/code]

and then the members index page
[code]
<?php
session_start();
//if there is no session variable, go to signin page
if (isset($_SESSION["username"]) == 1){
$username = $_SESSION["username"];
}else{
header ('Location: signin.php');
}
include('head.html');
echo $_SESSION["username"];
include('font.html');
include('foot.html');
?>
[/code]

The only problem is that it does not echo the username!, yet I am still directed to the index page.  Anyone know what is wrong?

EDIT:  this is strange... suddenly it doesnt work anymore.... when i go to sign in, at the bottom left it is requesting the page signinprocess.php, goes white for a minute, and then goes right back to the signin.php page... guess that makes it two problems  ??? :(

EDITEDIT:  duh to the above... the index page is requesting a session variable and if it doesn't have it it means it wont let me access the page  :D ::)
Link to comment
https://forums.phpfreaks.com/topic/29053-php-session-help/
Share on other sites

[quote author=Crayon Violent link=topic=116923.msg476680#msg476680 date=1164938998]
move this:

$_SESSION["username"] = "$username";

to here:
[code]
if (mysql_num_rows($result) != 1) {
include("signin.php?error=invalid");
exit();
}else {
  $_SESSION["username"] = $username;
  header('Location: memers/index.php');
}
?>
[/code]
[/quote]
First, $_SESSION["username"] = "$username"; is bad.  Double quotes will parse "$variables", but why bother creating the extra work?  $_SESSION['username'] = $username; is all you need to do.  Second, your problem is that you're assigning $username to $_SESSION['username'], except this is at the top of your script, and $username doesn't have any kind of a value to it.  So you're basically saying $_SESSION['username'] = '';  Maybe you meant to put $_POST['username'] ?
Link to comment
https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133213
Share on other sites

lol thats probably why it used to sign me in... i was wondering how it could sign me in but echo no variable - must have just been echoing nothing :P

even moving that down, it still doesn't work... i appreciate the help though :)

I am very new to the whole idea of sessions, but basically I'm thinking $_SESSION its similar to saying $_POST... except the variables are stored somewhere magical where you can alwasy get them..???  but heres the code with a few revisions

[code]
<?php
//signinprocess.php
session_start();
$username = $_POST['username'];
$password = $_POST['password'];

// make mysql connection
mysql_connect("server", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$query = "SELECT * FROM hsmemberdb WHERE username='$username' and emailpass='$password'";

$result = mysql_query($query);

if (mysql_num_rows($result) == 1) {
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
include('members/index.php');
}else {
header('Location: signin.php?error=invalid');
exit();
}
?>
[/code]

/members/index.php
[code]
<?php
session_start();
if (empty($_SESSION["username"])) {
header ('Location: signin.php?error=sessionerror');
exit();
}else{
$username = $_SESSION["username"];
}
include('../head.html');
echo $_SESSION["username"];
include('../font.html');
include('../foot.html');
?>
[/code]

i changed to empty() instead of isset() just for a test, but neither worked  :-\.  I also added the sessionerror part just so i could see for myself, but as of now im pretty much stuck...
Link to comment
https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133222
Share on other sites

[code]
// make mysql connection
mysql_connect("server", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());

$query = "SELECT * FROM hsmemberdb WHERE username='$username' and emailpass='$password'";
[/code]
You put server, username and password??? Did you just remove the variables or what, because it's not going to connect like that.  Also on your queries, change your queries to like msyql_query($query) or die('Error: '.mysql_error()); and it'll let you know if your SQL syntax is bad. 

[code]if (empty($_SESSION["username"])) {
header ('Location: signin.php?error=sessionerror');
exit();
}else{
$username = $_SESSION["username"];
}[/code]
Try:
[code]
if (!$_SESSION['username']){
header('Location: signin.php?error=sessionerror');
exit;
}
else
echo 'User is: '.$_SESSION['username'];
[/code]
Let me know what you get for that.
Link to comment
https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133227
Share on other sites

[quote author=projectshifter link=topic=116923.msg476789#msg476789 date=1164952737]
You put server, username and password???[/quote]

lol mysql may not be my best language, but im not THAT bad at it :)

I have a feeling this error is on the index.php page and not the signinprocess.php, but thats just me

anyways, it still shows the same error, it won't go to the index page
Link to comment
https://forums.phpfreaks.com/topic/29053-php-session-help/#findComment-133230
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.