Hi everybody!

Please help me very fast!!!!

I am programming with php in linux platform.

When I create a file (example index.php) , that is UTF8 as default.

Now I want to set cookie or session , but I cannot! I get an error about sending headers!

Please help me how to set cookie and session in UTF8 files!!!!


Thanks a lot.


----Morteza

Recommended Answers

All 11 Replies

Showing us some code would help us better understand the situation and what you're doing.

Showing us some code would help us better understand the situation and what you're doing.

<?php
        if($_POST)
        {
            
            $con=mysql_connect('localhost','root','136');
            mysql_select_db('phpshop',$con);
            if($_POST['group']=='seller'){
            $query=mysql_query("select shop_username,shop_password from shopping where shop_username='$_POST[username]' AND shop_password='$_POST[password]'");
            $row=mysql_fetch_array($query);
            if(!empty ($row))
            {
                setcookie('status','seller',time()+1600);
                echo "Welcome Seller";
            }else{
                echo "invalid seller";
            }

            }
            if($_POST['group']=='admin'){
            $query=mysql_query("select username,password from administrator where username='$_POST[username]' AND password='$_POST[password]'");
            $row=mysql_fetch_array($query);
            if(!empty ($row))
            {
                setcookie('status','administrator',time()+1600);
                echo "Welcome Admin";
            }else{
                echo "Invalid username or pass!";
            }

            }

            mysql_close($con);
            }
        
        ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8" >
        <title></title>
    </head>
    <body>
        <form method="post" action="<?php $PHP_SELF; ?>">
        <table>
            <tr>
                <td>Username:</td>
                <td><input type="text" name="username" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>
                <td>Group:</td>
                <td>
                    <select name="group">
                        <option value="seller">Seller</option>
                        <option value="admin">Administrator</option>
                   </select>
                </td>
            </tr>
            <tr>
                <td align="right" colspan="2"><input type="submit" value="Login" /></td>
            </tr>
        </table>
        </form>
    </body>
</html>

Please help me how to set cookie and session in UTF8 files!!!!

Thanks again . :)

If that first blank line is actually in your source file, that is the culprit. There can be NO OUTPUT before setting a cookie or session (which also sets a cookie.)

When I type setcookie function at the first line I see header error!

Can you modify this code?

Member Avatar for diafol

You have welcome seller/invalid seller output before setting a cookie. You were already told that there could not be any output before setting the cookie.

Thanks.

What do I do now?

Member Avatar for diafol

Sorry, scratch that, you're echoing after the setcookie, so the error must be before that. Seems that you have some whitespace before the php tag. Get rid of this.

I have an error again when I use setcookie function in the first line !!

<?php
setcookie('status','seller',time()+1600);
        if($_POST)
        {
 
            $con=mysql_connect('localhost','root','136');
            mysql_select_db('phpshop',$con);
            if($_POST['group']=='seller'){
.....
Member Avatar for diafol

Do you have white space before the <?php tag

Whitespace inside the php tag doesn't matter

Thanks a lot!!!!

I had a white space before the <?php tag.
When I removed that , code work correctly!

Ok. So we can setcookie or session in UTF8 files!


Thanks again!

Member Avatar for diafol

OK, mark this thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.