Jump to content

Recommended Posts

Hi i have made a login in system for a website iam trying to make. after you log  in im trying to display the members username via the $_session created in the check_login.php. but when i Echo or print_r the $_session all is get is  "welcome  array" its like its not passing any information via the $_session from page to page. here is my code thanks in advance.

 

 

Check_login.php

session_start();

 

// username and password sent from form

$myusername=$_POST['myusername'];

$mypassword=md5($_POST["mypassword"]);

 

// To protect MySQL injection (more detail about MySQL injection)

$myusername = stripslashes($myusername);

$mypassword = stripslashes($mypassword);

$myusername = mysql_real_escape_string($myusername);

$mypassword = mysql_real_escape_string($mypassword);

 

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";

$result=mysql_query($sql);

 

// Mysql_num_row is counting table row

$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

 

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"

$_SESSION["myusername"]==$myusername;

$_SESSION["mypassword"];

 

header("location:login_success.php");

}

else {

echo "Wrong Username or Password";

}

 

?>

 

login_success.php

 

<?

session_start();

if($_SESSION['myusername']="$myusername"){

header("location:main_login.php");

}

 

Echo "welcome" . $_SESSION['$myusername'];

 

?>

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/233459-php-_session-help-thanks/
Share on other sites

I think you have your assignment/equal to operators confused a little. "=" is for assigning a variable. "==" is for checking if a something is equal to something else. I think your problem is right here:

$_SESSION["myusername"]==$myusername;

That only checks if the two are equal, it doesn't set it like you want.

 

And here:

 if($_SESSION['myusername']="$myusername"){

That actually checks if $_SESSION['myusername'] was successfully set to $myusername.

 

So try this for check_login.php

<?php
session_start();

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=md5($_POST["mypassword"]);

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION["myusername"] = $myusername; //copy $myusername to $_SESSION['myusername']
$_SESSION["mypassword"] = $mypassword; //copy $mypassword to $_SESSION['mypassword']

header("location: login_success.php");
}
else {
echo "Wrong Username or Password";
}

?>

and this for login_success.php

<? 
session_start();
if($_SESSION['myusername'] != $myusername){ //if usernames don't match (is this what you wanted??)
header("location: main_login.php");
}

Echo "welcome " . $_SESSION['$myusername'];

?>

By the way, where's $tbl_name come from exactly?

 

PS: use


tags for your code next time please.

hi thanks for your help... i just tried copied and pasted your code and tried it but it it just Echoed Welcome and nothing else.  there is a sign up page which creates the username and password and small profile and picture for the user. now what I am wanting to is after the user logs in it will bring up there profile they created and saved to database. but if i cant access the user name from the SESSION i wont be able to work out which user has logged in.

 

i did write the code in the dreamweaver cs4 editor and it was Charset utf 8 which i was reading could cause a problem but i have since remove the charset utf 8 and still cant get the username to display thru the SESSION.

 

 

hope that makes sense

thanks again

i have read many different tutorials from all over the place trying to make sense of it all,  trying all different things till something works where is good place to for tutorials?  I just discovered if i refresh the page it logs me out how can i keep the user logged if they refresh the page?

If you're just learning, I'd recommend buying a current book. Tutorials generally are for one specific process, and unless you already have a grasp of the language, you can't possibly know if the code they're pushing is any good or not until it's too late.

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.