[solved] forgot session_start

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
m2babaey
Forum Contributor
Posts: 364
Joined: Sun May 20, 2007 9:26 am

[solved] forgot session_start

Post by m2babaey »

Hi
I want to insert the posted article into database plus the username of sender using this code:

Code: Select all

<?php
$title = $_POST['title'];
$subject = $_POST['subject'];
$article =$_POST['article'];
$username =$_SESSION['username'];
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("articles") or die(mysql_error());
mysql_query("INSERT INTO articles (subject, title, text ,username  ) VALUES('$subject','$title','$article','$username' )")
or die(mysql_error());  
mysql_close();
?>
But it says:
Notice: Undefined variable: _SESSION in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\another\htdocs\sendarticle.php on line 6
and the username is not sent. why is is not working?
Last edited by m2babaey on Tue Jul 03, 2007 12:23 pm, edited 1 time in total.
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Post by kaszu »

If you are certain that $_SESSION['username'] exists, then only problem i see here is that session is not started, you need to call a session_start() to use it.
Also your script is vulnerable to SQL Injections.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can you please edit your post title? 'Who is posting' has nothing to do with the real problem of you getting an undefined variable error.
Post Reply