473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Why will this session not pass through?

65
Hi all,

The following code works in that it will pass through a session variable of email, which comes from a posted value.

When i add the while loop to extract the user type, it wont pass it through.

any ideas?

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. ob_start();
  4.  
  5.  
  6. // Connect to server and select databse.
  7. mysql_connect("$host", "$username", "$password")or die("cannot connect");
  8. mysql_select_db("$db_name")or die("cannot select DB");
  9.  
  10. // username and password sent from form
  11. $user_email=$_POST['user_email'];
  12. $user_password=$_POST['user_password'];
  13.  
  14. // To protect MySQL injection (more detail about MySQL injection)
  15. $user_email = stripslashes($user_email);
  16. $user_password = stripslashes($user_password);
  17. $user_email = mysql_real_escape_string($user_email);
  18. $user_password = mysql_real_escape_string($user_password);
  19.  
  20. $sql="SELECT * FROM $tbl_name WHERE user_email='$user_email' and user_password='$user_password'";
  21.  
  22. while($row = mysql_fetch_array($query))
  23. {
  24.     $user_type = $row[1];
  25.     echo $user_type;
  26.     exit ();
  27. }
  28.  
  29. $result=mysql_query($sql);
  30.  
  31. // Mysql_num_row is counting table row
  32. $count=mysql_num_rows($result);
  33. // If result matched $myusername and $mypassword, table row must be 1 row
  34.  
  35. if($count==1){
  36. // Register $myusername, $mypassword and redirect to file "login_success.php"
  37. session_start();
  38. $_SESSION['user_email'] = $user_email;
  39. $_SESSION['user_type'] = $user_type;
  40. header("location:login_success.php");
  41. }
  42. else {
  43. echo "Wrong Username or Password";
  44. }
  45. ?>
  46.  
Feb 2 '10 #1
5 1817
Atli
5,058 Expert 4TB
Hey.

The mysql_fetch_array call on line #22 is referencing a variable called $query, but I do not see it defined anywhere.

If you want to use the result of the query defined as $sql, you will need to actually execute the query before the loop and use the result from that execution.

O, and also, if you only need to use a single row from a result set, you might want to add a LIMIT to your query and loose the while loop. A loop is only needed if you plan to loop through several pieces of data. If you only need one row, something like this would be better.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $sql = "SELECT stuff FROM tbl WHERE other='stuff' LIMIT 1";
  3. $result = mysql_query($sql) or trigger_error(mysql_error(), E_ERROR);
  4. $row = mysql_fetch_array($result);
  5.  
  6. $stuff = $row[0];
  7. ?>
Feb 2 '10 #2
whitep8
65
Hi Thanks for the info. Ive made the changes you suggest.

Can you see any issues with the session registers?

this is the success file, but only the email is echoing

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. session_start();
  3.     echo "Hello ";
  4.     echo $_SESSION['user_email'];
  5.     echo $_SESSION['user_type'];
  6. ?>
  7.  
  8.  
Feb 2 '10 #3
whitep8
65
just out of interest, i pushed the password into a session variable, and the echo'ed ok, so its literally the user type variable that wont go through
Feb 2 '10 #4
Atli
5,058 Expert 4TB
I see no issues with that part of the code, no. I should be set fine, unless the $user_type variable is somehow not being given a proper value.
Have you tried to var_dump it before setting the session, just to see what is in there?
Feb 3 '10 #5
whitep8
65
Hi Atli,

I removed the page header and echo'd out everything, i just couldnt find why SO......

i moved the sql to identify the user type and cast to a session at the top of the next page, which worked fine. I needed the user type to use in a switch statement, which now all works fine.

thanks for you help everybody
Feb 3 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Pete | last post by:
I have a site which is using sessions to pass data from one page through to the next. The problem that I have is that there are only two places where the session could/should be destroyed. This...
3
by: Peter Redding | last post by:
Forgive me if this doesn't make too much sense but I've been working on this project all day and my brain is a bit fried. To make some of my code more readable I made a file (called urls.inc)...
5
by: Arun | last post by:
Hi, Is it possible to share Session/Application State across different Asp.net webapplication (one in VB.net ) and the other is C# ? If so can you guide me ? -Regards' Arun
6
by: Angel | last post by:
I am trying to save private information such as user id and password from one application and send it to another application. I tried saving the information in the Session Object and when the user...
3
by: nn | last post by:
Hello I have ASP.Net application configured to use cookies for session. For some reasons I can't move to cookieless model. I need access to session created in this application without cookie. I...
3
by: masoud bayan | last post by:
Hi, We have 3 different web applications on three different websites (and domains). Now we want to make it possible for users to login in each of these applications and can navigate to other...
2
by: Tommy | last post by:
I have written some code to use paypal for users to make purchases from my site. I am using the paypal instant payment notification. When i get the notification from paypal I send the user an...
14
by: Coleen | last post by:
Hi All :-) We have an APSX application using VB.net as the code behind, which uses one or two session variables per page. These Session variables are passed to the final page and calculations...
1
by: windandwaves | last post by:
Hi I have the following file: --myfile.php <?php if($s) { echo $s; session_id($s); session_start;
1
by: Mikey C | last post by:
Hi there, I am using ASP.NET 2.0. The problem I am facing stems from the fact that the Flash plugin for Firefox does not have access to the browser's cookies. The flash component I am using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.